<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>docker镜像 | 如是的学习博客</title><meta name="keywords" content="docker"><meta name="author" content="rushiy"><meta name="copyright" content="rushiy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="镜像是容器的基石，容器基于镜像启动和运行。镜像保存的容器启动的各种条件(依赖)。Docker Image是一个层叠的只读文件系统。docker镜像具有以下特点：  Docker镜像采用分层构建机制，最底层是bootfs（引导文件系统）、其次是rootfs（用户文件系统）； bootfs用于系统引导的文件系统，包括boot loader和kernel，容器启动完成后会卸载以节约内存空间； rootf">
<meta property="og:type" content="article">
<meta property="og:title" content="docker镜像">
<meta property="og:url" content="https://rushiy.gitee.io/docker/docker%E9%95%9C%E5%83%8F/index.html">
<meta property="og:site_name" content="如是的学习博客">
<meta property="og:description" content="镜像是容器的基石，容器基于镜像启动和运行。镜像保存的容器启动的各种条件(依赖)。Docker Image是一个层叠的只读文件系统。docker镜像具有以下特点：  Docker镜像采用分层构建机制，最底层是bootfs（引导文件系统）、其次是rootfs（用户文件系统）； bootfs用于系统引导的文件系统，包括boot loader和kernel，容器启动完成后会卸载以节约内存空间； rootf">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-09-22T15:36:16.000Z">
<meta property="article:author" content="rushiy">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary"><link rel="shortcut icon" href="/img/favicon.jpg"><link rel="canonical" href="https://rushiy.gitee.io/docker/docker%E9%95%9C%E5%83%8F/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'docker镜像',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-19 01:18:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">如是的学习博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">docker镜像</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-22T15:36:16.000Z" title="发表于 2022-09-22 23:36:16">2022-09-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-18T17:18:40.902Z" title="更新于 2022-11-19 01:18:40">2022-11-19</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="docker镜像"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>镜像是容器的<strong>基石</strong>，容器基于镜像启动和运行。镜像保存的<strong>容器启动的各种条件(依赖)<strong>。<code>Docker Image</code>是一个</strong>层叠的只读文件系统</strong>。docker镜像具有以下特点：</p>
<ul>
<li>Docker镜像采用分层构建机制，最底层是<code>bootfs</code>（引导文件系统）、其次是<code>rootfs</code>（用户文件系统）；</li>
<li><code>bootfs</code>用于系统引导的文件系统，包括<code>boot loader</code>和<code>kernel</code>，<strong>容器启动完成后会卸载</strong>以节约内存空间；</li>
<li><code>rootfs</code>位于<code>bootfs</code>之上，表现为docker容器的根文件系统：传统模式中，系统启动之时，内核挂载rootfs时会首先将其挂载为“只读”模式，完整性自检完成后将其重新挂载为读写模式（Linux系统启动模式）；docker中，rootfs由内核挂载为“只读”模式，而后通过“联合挂载”技术额外挂载一个“可写”层；</li>
</ul>
<p>下图中，位于下层的镜像为父镜像（parent image），最底层成为基础镜像（base image），最上层为“可读写”层，其下的均为“只读”层。</p>
<p>![img]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;clip_image052.jpg)</p>
<ul>
<li><code>Aufs</code>（advanced multi-layered unification filesystem）：高级多层联合文件系统</li>
</ul>
<p>用于Linux文件系统实现<code>联合挂载</code>，docker最初使用aufs作为容器文件系统层，它目前仍作为存储后端之一来支持；aufs的竞争产品<code>overlayfs</code>，后者自从3.18版本开始被合并到Linux内核，docker的分层镜像，除了aufs，还支持<code>btrfs</code>、<code>devicemapper(dm)</code>和<code>vfs</code>等（在Ubuntu系统中，aufs合并到了Ubuntu系统中；但CentOS系统未合并，因此使用aufs系统需打补丁，CentOS默认使用的是overlayfs）</p>
<p>![img]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;clip_image054.jpg)</p>
<blockquote>
<p>联合加载技术是<strong>一次加载多个文件系统</strong>，但是在外面看来好像只有一个文件系统。最终将各层文件<strong>叠加到一起</strong>，最终的文件系统包含所有的底层文件和目录。  </p>
<p>一个镜像可以放到另一个镜像的顶部，称为下一个镜像的<strong>父镜像</strong>。最底部的镜像称为<strong>基础镜像</strong>。</p>
</blockquote>
<h2 id="docker官方仓库的使用"><a href="#docker官方仓库的使用" class="headerlink" title="docker官方仓库的使用"></a>docker官方仓库的使用</h2><p>镜像仓库(用于镜像存放)官网：<a target="_blank" rel="noopener" href="https://hub.docker.com/">https://hub.docker.com/</a></p>
<p>官网的使用和简介：</p>
<ul>
<li>搜索指定镜像，任何人都可以注册使用docker镜像仓库，上传下载和使用镜像。</li>
</ul>
<p>![image-20200716151141854]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716151141854.png)</p>
<p>点击进入官方nginx镜像仓库：</p>
<p>![image-20200716151648043]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716151648043.png)</p>
<p><code>Reviews</code>：展示用户对镜像的评价信息。</p>
<p>![image-20200716153928518]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716153928518.png)</p>
<p><code>Tags</code>：展示镜像的所有标签信息。</p>
<p>![image-20200716151921503]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716151921503.png)</p>
<p>唯一标识一个镜像的方法：</p>
<ul>
<li>镜像id：sha256:64位，默认只截取12位</li>
<li>镜像标签：<code>tag</code></li>
</ul>
<h2 id="docker镜像相关命令"><a href="#docker镜像相关命令" class="headerlink" title="docker镜像相关命令"></a>docker镜像相关命令</h2><p>有关镜像的相关命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image --<span class="built_in">help</span></span></span><br><span class="line"></span><br><span class="line">Usage:	docker image COMMAND</span><br><span class="line"></span><br><span class="line">Manage images</span><br><span class="line"></span><br><span class="line">Commands:</span><br><span class="line">  build       Build an image from a Dockerfile  # 通过Dockerfile文件构建一个镜像</span><br><span class="line">  history     Show the history of an image      # 构建镜像的历史命令</span><br><span class="line">  import      Import the contents from a tarball to create a filesystem image</span><br><span class="line">  inspect     Display detailed information on one or more images  # 查看镜像的详细信息</span><br><span class="line">  load        Load an image from a tar archive or STDIN  # 导入一个tar包的镜像文件</span><br><span class="line">  ls          List images</span><br><span class="line">  prune       Remove unused images  # 移除没使用的镜像</span><br><span class="line">  pull        Pull an image or a repository from a registry   # 拉取镜像，默认是docker hub仓库</span><br><span class="line">  push        Push an image or a repository to a registry     # 推送镜像到docker仓库，默认是docker hub</span><br><span class="line">  rm          Remove one or more images    # 删除镜像</span><br><span class="line">  save        Save one or more images to a tar archive (streamed to STDOUT by default)# 将镜像保存为tar文件</span><br><span class="line">  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE  # 给镜像打标签</span><br><span class="line"></span><br><span class="line">Run &#x27;docker image COMMAND --help&#x27; for more information on a command.</span><br></pre></td></tr></table></figure>

<p>详细解释如下</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>解释</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><strong>build</strong></td>
<td>Build an image from a Dockerfile</td>
<td>从Dockerfile生成镜像</td>
</tr>
<tr>
<td><strong>history</strong></td>
<td>Show the history of an image</td>
<td>历史展示一个镜像的历史</td>
</tr>
<tr>
<td><strong>import</strong></td>
<td>Import the contents from a tarball to create a filesystem image</td>
<td>导入从tarball导入内容以创建文件系统镜像</td>
</tr>
<tr>
<td><strong>inspect</strong></td>
<td>Display detailed information on one or more images</td>
<td>检查显示一个或多个镜像的详细信息</td>
</tr>
<tr>
<td><strong>load</strong></td>
<td>Load an image from a tar archive or STDIN</td>
<td>从tar存档或stdin加载镜像</td>
</tr>
<tr>
<td><strong>ls</strong></td>
<td>List images</td>
<td>列出系统上现有的镜像</td>
</tr>
<tr>
<td><strong>prune</strong></td>
<td>Remove unused images</td>
<td>删除未使用的镜像</td>
</tr>
<tr>
<td><strong>pull</strong></td>
<td>Pull an image or a repository from a registry</td>
<td>从仓库中拉取镜像</td>
</tr>
<tr>
<td><strong>push</strong></td>
<td>Push an image or a repository to a registry</td>
<td>将镜像推送到远端仓库中</td>
</tr>
<tr>
<td><strong>rm</strong></td>
<td>Remove one or more images</td>
<td>rm删除一个或多个镜像</td>
</tr>
<tr>
<td><strong>save</strong></td>
<td>Save one or more images to a tar archive (streamed to STDOUT by  default)</td>
<td>将一个或多个镜像保存到tar存档（默认情况下流式传输到stdout）</td>
</tr>
<tr>
<td><strong>tag</strong></td>
<td>Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE</td>
<td>给镜像打标签</td>
</tr>
</tbody></table>
<h2 id="docker镜像常用操作"><a href="#docker镜像常用操作" class="headerlink" title="docker镜像常用操作"></a>docker镜像常用操作</h2><h3 id="下载镜像"><a href="#下载镜像" class="headerlink" title="下载镜像"></a>下载镜像</h3><h4 id="查找镜像"><a href="#查找镜像" class="headerlink" title="查找镜像"></a>查找镜像</h4><ul>
<li>图形化查找镜像</li>
</ul>
<p>首先，我们可以去<code>docker hub</code>官网进行查找指定镜像，如我们查找centos 7.6的镜像，找到后就可以直接复制红框中的命令进行下载。</p>
<p>![image-20200716214404996]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716214404996.png)</p>
<ul>
<li>命令行查找镜像</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">当然，我们也可以在测试机上进行简单的搜索</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker search  centos</span></span><br><span class="line">NAME   DESCRIPTION                   STARS     OFFICIAL   AUTOMATED   </span><br><span class="line">centos  The official build of CentOS.  6090     [OK]                </span><br><span class="line">ansible/centos7-ansible   Ansible on Centos7    132        [OK]</span><br><span class="line">····</span><br></pre></td></tr></table></figure>

<h4 id="下载镜像-1"><a href="#下载镜像-1" class="headerlink" title="下载镜像"></a>下载镜像</h4><ul>
<li>下载最新版本镜像</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">下载centos官方镜像，不指定标签则默认拉取最新的镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker pull centos</span></span><br><span class="line">latest: Pulling from library/centos</span><br><span class="line">6910e5a164f7: Pull complete </span><br><span class="line">Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b</span><br><span class="line">Status: Downloaded newer image for centos:latest</span><br><span class="line">docker.io/library/centos:latest</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">下载后的镜像一般存储在/var/lib/docker中</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">ls</span> /var/lib/docker</span></span><br><span class="line">builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes</span><br></pre></td></tr></table></figure>

<ul>
<li>下载指定版本镜像</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker pull centos:centos7.6.1810</span> </span><br><span class="line">centos7.6.1810: Pulling from library/centos</span><br><span class="line">ac9208207ada: Pull complete </span><br><span class="line">Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6</span><br><span class="line">Status: Downloaded newer image for centos:centos7.6.1810</span><br><span class="line">docker.io/library/centos:centos7.6.1810</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span> </span><br><span class="line">REPOSITORY      TAG     IMAGE ID       CREATED             SIZE</span><br><span class="line">nginx           latest  0901fa9da894        5 days ago          132MB</span><br><span class="line">centos          latest  831691599b88        4 weeks ago         215MB</span><br><span class="line">centos          centos7.6.1810  f1cb7c7d58b7 16 months ago      202MB</span><br></pre></td></tr></table></figure>

<h3 id="打标签"><a href="#打标签" class="headerlink" title="打标签"></a>打标签</h3><ul>
<li>通过镜像名称和现有标签打标签：<code>docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]</code></li>
<li>通过镜像id打标签：<code>docker image tag SOURCE_IMAGE_ID TARGET_IMAGE[:TAG]</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image tag --<span class="built_in">help</span></span></span><br><span class="line">Usage:	docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]</span><br><span class="line">Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">nginx        latest     0901fa9da894  5 days ago   132MB</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image tag 0901fa9da894 scaven/nginx:1.1.1</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">nginx        latest     0901fa9da894  5 days ago   132MB</span><br><span class="line">scaven/nginx 1.1.1      0901fa9da894  5 days ago   132MB</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br></pre></td></tr></table></figure>

<h3 id="删除镜像"><a href="#删除镜像" class="headerlink" title="删除镜像"></a>删除镜像</h3><blockquote>
<p>建议使用<code>镜像:标签</code>或者<code>镜像ID</code>来删除镜像，因为这样才是唯一标识了一个镜像</p>
</blockquote>
<ul>
<li>格式：<code>docker image rm [OPTIONS] IMAGE [IMAGE...]</code>或<code>docker rmi IMAGE</code>。</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用镜像ID删除镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">rm</span> 0901fa9da894</span></span><br><span class="line">Error response from daemon: conflict: unable to delete 0901fa9da894 (must be forced) - image is referenced in multiple repositories</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">报错的意思是该镜像<span class="built_in">id</span>标识了两个仓库的镜像，因此不能使用该方法删除，由此可知要删除两个镜像<span class="built_in">id</span>一样的镜像，只能使用`镜像仓库:标签`的方法</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">rm</span> scaven/nginx:1.1.1</span></span><br><span class="line">Untagged: scaven/nginx:1.1.1</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">nginx        latest     0901fa9da894  5 days ago   132MB</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">rm</span> 0901fa9da894</span></span><br><span class="line">Error response from daemon: conflict: unable to delete 0901fa9da894 (must be forced) - image is being used by stopped container 2a2bbb830211</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">报错是指，该镜像关联了一个停止中的容器，需要使用-f参数才能强制删除，如果是一个运行中的容器，不能强制删除</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用-f参数，强制删除容器镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">rm</span> -f 0901fa9da894</span></span><br><span class="line">Untagged: nginx:latest</span><br><span class="line">Deleted: sha256:0901fa9da894a8e9de5cb26d6749eaffb67b373dc1ff8a26c46b23b1175c913a</span><br><span class="line">[root@docker ~]# docker image ls </span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">重启被删除镜像的容器，容器运行正常</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container start 79a9b0e8c074</span></span><br><span class="line">79a9b0e8c074</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps</span> </span><br><span class="line">CONTAINER ID  IMAGE   COMMAND CREATED   STATUS   PORTS    NAMES</span><br><span class="line">79a9b0e8c074        0901fa9da894        &quot;/docker-entrypoint.…&quot;   19 hours ago        Up 39 seconds       0.0.0.0:32768-&gt;80/tcp   webapp</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">删除全部镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">rm</span> -f `docker image <span class="built_in">ls</span> -q`    <span class="comment"># 其中`docker image ls -q`是显示所有镜像的镜像ID</span></span></span><br></pre></td></tr></table></figure>

<h3 id="导入和导出镜像"><a href="#导入和导出镜像" class="headerlink" title="导入和导出镜像"></a>导入和导出镜像</h3><ul>
<li>导出镜像：<code>docker image save -o IMAGE.tar IMAGE [IMAGE...]</code></li>
<li>导入镜像：<code>docker image load -i IMAGE.tar</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在删除nginx镜像之前做了导出镜像，如下</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image save 0901fa9da894 &gt; /tmp/nginx.tar</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">ls</span> /tmp/nginx.tar</span> </span><br><span class="line">/tmp/nginx.tar</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在另外一台docker主机上导入镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker load -i nginx.tar</span></span><br><span class="line">Loaded image ID: sha256:0901fa9da894a8e9de5cb26d6749eaffb67b373dc1ff8a26c46b23b1175c913a</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">&lt;none&gt;        &lt;none&gt;    0901fa9da894  5 days ago   132MB</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用docker image save 0901fa9da894 &gt; /tmp/nginx.tar这个命令导出的镜像文件在导入时，不能继承源镜像的仓库和标签，需要打标签</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">给没有指定仓库和标签的镜像打标签，此处只能使用镜像ID来标识该镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image tag 0901fa9da894 scaven/nginx:1.2</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">scaven/nginx 1.2        0901fa9da894  5 days ago   132MB</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br></pre></td></tr></table></figure>

<h3 id="上传自制镜像到docker官方仓库"><a href="#上传自制镜像到docker官方仓库" class="headerlink" title="上传自制镜像到docker官方仓库"></a>上传自制镜像到docker官方仓库</h3><h4 id="创建镜像仓库"><a href="#创建镜像仓库" class="headerlink" title="创建镜像仓库"></a>创建镜像仓库</h4><blockquote>
<p>此处以centos镜像为例，仓库名称应该具有描述性。</p>
</blockquote>
<p>![image-20200716221423032]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716221423032.png)</p>
<p>创建完成后，如下：</p>
<p>![image-20200716221551532]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716221551532.png)</p>
<h4 id="登录docker官方仓库"><a href="#登录docker官方仓库" class="headerlink" title="登录docker官方仓库"></a>登录<code>docker</code>官方仓库</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker login</span></span><br><span class="line">Login with your Docker ID to push and pull images from Docker Hub. If you don&#x27;t have a Docker ID, head over to https://hub.docker.com to create one.</span><br><span class="line">Username: scaven  # 输入用户名</span><br><span class="line">Password:         # 输入密码</span><br><span class="line">WARNING! Your password will be stored unencrypted in /root/.docker/config.json.</span><br><span class="line">Configure a credential helper to remove this warning. See</span><br><span class="line">https://docs.docker.com/engine/reference/commandline/login/#credentials-store</span><br><span class="line"></span><br><span class="line">Login Succeeded   # 提示登录成功</span><br></pre></td></tr></table></figure>

<h4 id="打标签-1"><a href="#打标签-1" class="headerlink" title="打标签"></a>打标签</h4><blockquote>
<p>将本地镜像中的仓库名称修改成docker官方镜像仓库中的仓库名称</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image tag centos:centos7.6.1810 scvaen/os:centos7.6.1810</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY    TAG       IMAGE ID        CREATED             SIZE</span><br><span class="line">nginx         latest    0901fa9da894    5 days ago          132MB</span><br><span class="line">centos        latest    831691599b88        4 weeks ago     215MB</span><br><span class="line">centos        centos7.6.1810  f1cb7c7d58b7  16 months ago   202MB   ## 此时有两个一模一样的镜像</span><br><span class="line">scaven/os     centos7.6.1810  f1cb7c7d58b7  16 months ago   202MB</span><br></pre></td></tr></table></figure>

<h4 id="推送镜像到docker官方仓库"><a href="#推送镜像到docker官方仓库" class="headerlink" title="推送镜像到docker官方仓库"></a>推送镜像到docker官方仓库</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker push scaven/os:centos7.6.1810</span></span><br><span class="line">The push refers to repository [docker.io/scaven/os]</span><br><span class="line">89169d87dbe2: Mounted from library/centos </span><br><span class="line">centos7.6.1810: digest: sha256:747b2de199b6197a26eb1a24d69740d25483995842b2d2f75824095e9d1d19eb size: 529</span><br></pre></td></tr></table></figure>

<h4 id="检查镜像推送结果"><a href="#检查镜像推送结果" class="headerlink" title="检查镜像推送结果"></a>检查镜像推送结果</h4><p>命令执行成功后，我们可以在镜像仓库中查看到已经上传的镜像信息，如下：<br>![image-20200716223356907]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200716223356907.png)</p>
<h3 id="查看镜像的详细信息"><a href="#查看镜像的详细信息" class="headerlink" title="查看镜像的详细信息"></a>查看镜像的详细信息</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY   TAG        IMAGE ID      CREATED      SIZE</span><br><span class="line">scaven/nginx 1.2        0901fa9da894  5 days ago   132MB</span><br><span class="line">ubuntu       15.10      9b9cb95443b5  3 years ago  137MB</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image inspect 0901fa9da894</span>  </span><br><span class="line">[</span><br><span class="line">    &#123;</span><br><span class="line">        &quot;Id&quot;: &quot;sha256:0901fa9da894a8e9de5cb26d6749eaffb67b373dc1ff8a26c46b23b1175c913a&quot;,</span><br><span class="line">        &quot;RepoTags&quot;: [  # 镜像标签新信息</span><br><span class="line">            &quot;scaven/nginx:1.2&quot;</span><br><span class="line">        ],</span><br><span class="line">        &quot;RepoDigests&quot;: [],</span><br><span class="line">        &quot;Parent&quot;: &quot;&quot;,</span><br><span class="line">        &quot;Comment&quot;: &quot;&quot;,</span><br><span class="line">        &quot;Created&quot;: &quot;2020-07-10T20:26:44.624785651Z&quot;,  # 创建时间</span><br><span class="line">        &quot;Container&quot;: &quot;348c3ade7f4bdc0366f3f390ea4cfaebfb355ad7d621547eaf73728136d3bd2d&quot;,</span><br><span class="line">        &quot;ContainerConfig&quot;: &#123;  # 容器配置信息</span><br><span class="line">            &quot;Hostname&quot;: &quot;348c3ade7f4b&quot;,  # 启动后的主机名--镜像ID前12位</span><br><span class="line">            &quot;Domainname&quot;: &quot;&quot;,</span><br><span class="line">            &quot;User&quot;: &quot;&quot;,</span><br><span class="line">            &quot;AttachStdin&quot;: false,</span><br><span class="line">            &quot;AttachStdout&quot;: false,</span><br><span class="line">            &quot;AttachStderr&quot;: false,</span><br><span class="line">            &quot;ExposedPorts&quot;: &#123;</span><br><span class="line">                &quot;80/tcp&quot;: &#123;&#125;   # 启动的端口</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;Tty&quot;: false,</span><br><span class="line">            &quot;OpenStdin&quot;: false,</span><br><span class="line">            &quot;StdinOnce&quot;: false,</span><br><span class="line">            &quot;Env&quot;: [  # 容器环境变量信息</span><br><span class="line">                &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;,</span><br><span class="line">                &quot;NGINX_VERSION=1.19.1&quot;,</span><br><span class="line">                &quot;NJS_VERSION=0.4.2&quot;,</span><br><span class="line">                &quot;PKG_RELEASE=1~buster&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;Cmd&quot;: [  # 启动后指定的指定命令</span><br><span class="line">                &quot;/bin/sh&quot;,</span><br><span class="line">                &quot;-c&quot;,</span><br><span class="line">                &quot;#(nop) &quot;,</span><br><span class="line">                &quot;CMD [\&quot;nginx\&quot; \&quot;-g\&quot; \&quot;daemon off;\&quot;]&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;ArgsEscaped&quot;: true,</span><br><span class="line">            &quot;Image&quot;: &quot;sha256:8a6dfc8c21a1b3f3679b7755fc7869a22b5f8583778cf7835b5ee5387a73ae5e&quot;,</span><br><span class="line">            &quot;Volumes&quot;: null,   # 存储卷信息</span><br><span class="line">            &quot;WorkingDir&quot;: &quot;&quot;,  # 工作目录</span><br><span class="line">            &quot;Entrypoint&quot;: [</span><br><span class="line">                &quot;/docker-entrypoint.sh&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;OnBuild&quot;: null,</span><br><span class="line">            &quot;Labels&quot;: &#123;</span><br><span class="line">                &quot;maintainer&quot;: &quot;NGINX Docker Maintainers &lt;docker-maint@nginx.com&gt;&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;StopSignal&quot;: &quot;SIGTERM&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;DockerVersion&quot;: &quot;18.09.7&quot;,  # docker版本信息</span><br><span class="line">        &quot;Author&quot;: &quot;&quot;,</span><br><span class="line">        &quot;Config&quot;: &#123;    # 配置信息</span><br><span class="line">            &quot;Hostname&quot;: &quot;&quot;,</span><br><span class="line">            &quot;Domainname&quot;: &quot;&quot;,</span><br><span class="line">            &quot;User&quot;: &quot;&quot;,</span><br><span class="line">            &quot;AttachStdin&quot;: false,</span><br><span class="line">            &quot;AttachStdout&quot;: false,</span><br><span class="line">            &quot;AttachStderr&quot;: false,</span><br><span class="line">            &quot;ExposedPorts&quot;: &#123;</span><br><span class="line">                &quot;80/tcp&quot;: &#123;&#125;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;Tty&quot;: false,</span><br><span class="line">            &quot;OpenStdin&quot;: false,</span><br><span class="line">            &quot;StdinOnce&quot;: false,</span><br><span class="line">            &quot;Env&quot;: [</span><br><span class="line">                &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;,</span><br><span class="line">                &quot;NGINX_VERSION=1.19.1&quot;,</span><br><span class="line">                &quot;NJS_VERSION=0.4.2&quot;,</span><br><span class="line">                &quot;PKG_RELEASE=1~buster&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;Cmd&quot;: [</span><br><span class="line">                &quot;nginx&quot;,</span><br><span class="line">                &quot;-g&quot;,</span><br><span class="line">                &quot;daemon off;&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;ArgsEscaped&quot;: true,</span><br><span class="line">            &quot;Image&quot;: &quot;sha256:8a6dfc8c21a1b3f3679b7755fc7869a22b5f8583778cf7835b5ee5387a73ae5e&quot;,</span><br><span class="line">            &quot;Volumes&quot;: null,</span><br><span class="line">            &quot;WorkingDir&quot;: &quot;&quot;,</span><br><span class="line">            &quot;Entrypoint&quot;: [</span><br><span class="line">                &quot;/docker-entrypoint.sh&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;OnBuild&quot;: null,</span><br><span class="line">            &quot;Labels&quot;: &#123;</span><br><span class="line">                &quot;maintainer&quot;: &quot;NGINX Docker Maintainers &lt;docker-maint@nginx.com&gt;&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;StopSignal&quot;: &quot;SIGTERM&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;Architecture&quot;: &quot;amd64&quot;,   # 架构</span><br><span class="line">        &quot;Os&quot;: &quot;linux&quot;,   # 操作系统 </span><br><span class="line">        &quot;Size&quot;: 132484492,  # 镜像大小</span><br><span class="line">        &quot;VirtualSize&quot;: 132484492,</span><br><span class="line">        &quot;GraphDriver&quot;: &#123;</span><br><span class="line">            &quot;Data&quot;: &#123;</span><br><span class="line">                &quot;LowerDir&quot;: &quot;/var/lib/docker/overlay2/9167e98f0c6eedbce79f55ad73db84fdbdc65a80dd0ff8b57e6d068aa8828c8d/diff:/var/lib/docker/overlay2/5908a0b08cecf7d93c0bfc6822ee52af619a872a4196c3091fffa2eb82588b0f/diff:/var/lib/docker/overlay2/dc038e093dc9db0c8996635b1d6cf79dae163ed7cd93922824fef535668fb84e/diff:/var/lib/docker/overlay2/93c919ef9bab4ad8c9b570275d0d8abcc1c530aea546e70630a9d14060b61195/diff&quot;,</span><br><span class="line">                &quot;MergedDir&quot;: &quot;/var/lib/docker/overlay2/78476f806a7f8215910d9d6f1a55aa7f432562dc3dca97d28e69f6e2e6d17149/merged&quot;,</span><br><span class="line">                &quot;UpperDir&quot;: &quot;/var/lib/docker/overlay2/78476f806a7f8215910d9d6f1a55aa7f432562dc3dca97d28e69f6e2e6d17149/diff&quot;,</span><br><span class="line">                &quot;WorkDir&quot;: &quot;/var/lib/docker/overlay2/78476f806a7f8215910d9d6f1a55aa7f432562dc3dca97d28e69f6e2e6d17149/work&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;Name&quot;: &quot;overlay2&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;RootFS&quot;: &#123;</span><br><span class="line">            &quot;Type&quot;: &quot;layers&quot;,</span><br><span class="line">            &quot;Layers&quot;: [</span><br><span class="line">                &quot;sha256:13cb14c2acd34e45446a50af25cb05095a17624678dbafbcc9e26086547c1d74&quot;,</span><br><span class="line">                &quot;sha256:0e32546a8af0cd04ad451d6a9d22e650e500e5da3636a32648c9f5aca96a0ff7&quot;,</span><br><span class="line">                &quot;sha256:7ef35766ef7d5d3d958022405b308d5c105b41190e1b63b2037c4055c6950c9e&quot;,</span><br><span class="line">                &quot;sha256:4856db5e4f59384c413c20c46cd5403a860e1b07c8fdbad24df1ffd9209d44e7&quot;,</span><br><span class="line">                &quot;sha256:2808ec4a8ea71c2660284d06cf7e25354b70b58504edb46ac3e705fb7e6ea519&quot;</span><br><span class="line">            ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;Metadata&quot;: &#123;   # 元数据</span><br><span class="line">            &quot;LastTagTime&quot;: &quot;2020-07-16T16:30:36.781611539+08:00&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="基于容器制做docker镜像"><a href="#基于容器制做docker镜像" class="headerlink" title="基于容器制做docker镜像"></a>基于容器制做docker镜像</h2><p>基于容器制作镜像的本质是在已经运行容器的基础上，在容器中添加新的文件或服务，将新添加的文件或服务保存到镜像。下面我们以在centos6和centos7中安装ssh服务和制作BBS网站为例制作docker镜像。</p>
<h3 id="制作一个拥有ssh服务的镜像"><a href="#制作一个拥有ssh服务的镜像" class="headerlink" title="制作一个拥有ssh服务的镜像"></a>制作一个拥有ssh服务的镜像</h3><h4 id="准备工作：制作本地局域网yum源"><a href="#准备工作：制作本地局域网yum源" class="headerlink" title="准备工作：制作本地局域网yum源"></a>准备工作：制作本地局域网yum源</h4><p>（1）安装并启动vsftpd</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">yum -y install vsftpd</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">systemctl <span class="built_in">enable</span> vsftpd &amp;&amp; systemctl start vsftpd</span></span><br></pre></td></tr></table></figure>

<p>（2）上传镜像到虚拟机</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">ls</span> /mnt/iso/</span></span><br><span class="line">CentOS-6.10-x86_64-bin-DVD1.iso  CentOS-7.6-x86_64-DVD-1810.iso</span><br></pre></td></tr></table></figure>

<blockquote>
<p>此处分别配置CentOS-6.10和CentOS-7.6的本地yum源</p>
</blockquote>
<p>（3）配置yum仓库</p>
<ul>
<li>配置CentOS-6.10的yum源</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">创建ftp镜像挂载目录</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">mkdir</span> -p /var/ftp/centos6.10</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">挂载光盘文件到ftp镜像挂载目录，使用<span class="string">&quot;-o loop&quot;</span>模式用来将一个档案当成硬盘分割挂上系统</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">mount -o loop /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso  /var/ftp/centos6.10/</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意，如果用IP地址访问不到，就用docker0的地址，默认是172.17.0.1</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">cat</span> &gt; /etc/yum.repos.d/ftp6.repo &lt;&lt; <span class="string">EOF</span></span></span><br><span class="line">[ftp6_10]</span><br><span class="line">name=ftp6_10</span><br><span class="line">baseurl=ftp://10.138.3.15/centos6.10</span><br><span class="line">enabled=1</span><br><span class="line">gpgcheck=0</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<ul>
<li>配置CentOS-7.6的yum源</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">mkdir</span> -p /var/ftp/centos7.6</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">mount -o loop /mnt/iso/CentOS-7.6-x86_64-DVD-1810.iso /var/ftp/centos7.6/</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">cat</span> &gt; /etc/yum.repos.d/ftp7.repo &lt;&lt; <span class="string">EOF</span></span></span><br><span class="line">[ftp7.6]</span><br><span class="line">name=ftp7.6</span><br><span class="line">baseurl=ftp://10.138.3.15/centos7.6</span><br><span class="line">enabled=1</span><br><span class="line">gpgcheck=0</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<p>浏览器访问如下所示：</p>
<p>![image-20200718181236918]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200718181236918.png)</p>
<h4 id="制作centos6的ssh服务镜像"><a href="#制作centos6的ssh服务镜像" class="headerlink" title="制作centos6的ssh服务镜像"></a>制作centos6的ssh服务镜像</h4><p>（1）在centos6.10中配置yum源并安装ssh服务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span> | grep centos</span></span><br><span class="line">centos    centos6.10     48650444e419     16 months ago    194MB</span><br><span class="line">centos    centos7.6.1810 f1cb7c7d58b7     16 months ago    202MB</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用centos6.10镜像启动容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -it -d --name centos6.10 48650444e419 /bin/bash</span></span><br><span class="line">625565a2013b5ec622764991d24161e45d580287e58397463c54801a26377483</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">进入centos6.10容器配置对于版本的yum源</span></span><br><span class="line">[root@625565a2013b /]# cat &gt; /etc/yum.repos.d/ftp6.repo &lt;&lt; EOF</span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">[ftp6_10]</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">name=ftp6_10</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">baseurl=ftp://172.17.0.1/centos6.10</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">enabled=1</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">gpgcheck=0</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">EOF</span></span><br><span class="line"></span><br><span class="line">[root@625565a2013b /]# yum clean all &amp;&amp; yum repolist</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装ssh服务并启动，以便通过ssh服务进行登录容器</span></span><br><span class="line">[root@625565a2013b /]# yum -y install openssh* net-tools</span><br><span class="line">[root@625565a2013b ~]# echo &quot;root:123456&quot; | chpasswd </span><br><span class="line">[root@625565a2013b ~]# ifconfig  | grep 172.17</span><br><span class="line">    inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0</span><br><span class="line">[root@625565a2013b ~]# service  sshd start</span><br><span class="line">Generating SSH2 RSA host key:                              [  OK  ]  # 生成了某些文件</span><br><span class="line">Generating SSH1 RSA host key:                              [  OK  ]  # 生成了某些文件</span><br><span class="line">Generating SSH2 DSA host key:                              [  OK  ]  # 生成了某些文件</span><br><span class="line">Starting sshd:                                             [  OK  ]</span><br><span class="line">[root@625565a2013b ~]# chkconfig  --add sshd</span><br></pre></td></tr></table></figure>

<p>（2）保存镜像</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker  commit centos6.10 scaven:cnetos6.10</span></span><br><span class="line">sha256:f1b33ecfb10763152ed80217b3a729b505983fecda4d22c19163c0a6bd2a9eb3</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span> | grep 6.10</span></span><br><span class="line">REPOSITORY   TAG      IMAGE ID      CREATED             SIZE</span><br><span class="line">scaven       cnetos6.10 f1b33ecfb107  7 seconds ago     485MB   # 通过docker commit保存的cnetos6.10镜像</span><br><span class="line">centos       centos6.10 48650444e419  16 months ago     194MB</span><br></pre></td></tr></table></figure>

<p>（3）启动容器并启动ssh服务</p>
<p>此时我们可以通过保存下来的镜像(保存了容器中的各种文件)启动一个新的容器，但是值得注意的是，启动该容器并不会有启动ssh服务(保存了容器中的各种文件，ssh服务的启动并没有在容器的文件中，因为我们是在命令行启动的ssh服务)，因为我们保存镜像时，保存的是当前的文件系统，并不能保存容器中的进程，因此，ssh服务是没有启动的，要么我们手动指定启动ssh服务(进入容器内启动或启动容器时在命令行指定)，要么我们将ssh添加到容器启动时的默认执行命令。</p>
<ul>
<li>进入容器内启动</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d  --name centos6_sshd f1b33ecfb107</span></span><br><span class="line">d6f755ad89cfaadae10fd92cd2ee1aaedebee00fd79c6b6673bf03ddc80e9428</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker <span class="built_in">exec</span> -it d6f755ad89cf /bin/bash</span></span><br><span class="line">[root@d6f755ad89cf /]# netstat  -lntp</span><br><span class="line">Active Internet connections (only servers)</span><br><span class="line">Proto Recv-Q Send-Q Local Address   Foreign Address  State  PID/Program name   </span><br><span class="line">[root@d6f755ad89cf /]# service sshd start</span><br><span class="line">Starting sshd:                                             [  OK  ]</span><br><span class="line">[root@d6f755ad89cf /]# netstat  -lntp</span><br><span class="line">Active Internet connections (only servers)</span><br><span class="line">Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name </span><br><span class="line">tcp   0      0      0.0.0.0:22    0.0.0.0:*       LISTEN   49/sshd       </span><br><span class="line">tcp   0      0      :::22         :::*            LISTEN   49/sshd  </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">ctrl p,q 退出</span></span><br></pre></td></tr></table></figure>

<ul>
<li>启动容器时命令行指定启动</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps</span></span><br><span class="line">CONTAINER ID   IMAGE      COMMAND    CREATED    STATUS   PORTS  NAMES</span><br><span class="line">d6f755ad89cf   f1b33ecfb107  &quot;/bin/bash&quot;  7 minutes ago  Up 7 minutes         centos6_sshd</span><br><span class="line">9d064f7416ec   f1cb7c7d58b7  &quot;/bin/bash&quot;  About an hour ago Up About an hour  centos7.6</span><br><span class="line">625565a2013b   48650444e419  &quot;/bin/bash&quot;  About an hour ago  Up About an hour  centos6.10</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run  -d  --name centos6_ssh f1b33ecfb107 service sshd start</span></span><br><span class="line">a2a0ce8779e6724aeb861c5b667799c66e7e4cdf85c0ebed8eeebb77057ff3bf</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps</span></span><br><span class="line">CONTAINER ID   IMAGE      COMMAND    CREATED    STATUS   PORTS  NAMES</span><br><span class="line">d6f755ad89cf   f1b33ecfb107  &quot;/bin/bash&quot;  7 minutes ago  Up 7 minutes         centos6_sshd</span><br><span class="line">9d064f7416ec   f1cb7c7d58b7  &quot;/bin/bash&quot;  About an hour ago Up About an hour  centos7.6</span><br><span class="line">625565a2013b   48650444e419  &quot;/bin/bash&quot;  About an hour ago  Up About an hour  centos6.10</span><br></pre></td></tr></table></figure>

<p>从上我们可以看出，容器执行完<code>service sshd start</code>命令后就直接停止运行了(执行命令<code>service sshd start</code>的窗口退出，容器就停止运行)，那么我们是否可以将ssh服务放在前台启动，即启动ssh服务的窗口永远不关闭。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用/usr/sbin/sshd -D命令可以将ssh服务前台启动，即窗口一直不退出，夯在前台，因此容器也不会执行完命令就退出</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -it -d  --name centos6_ssh_d f1b33ecfb107 /usr/sbin/sshd -D</span></span><br><span class="line">38990dc22fec4efd91d477f08869c7f87681663ee668b68394fc58860ecc5ecd</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">加上-p 22参数就能暴露端口，实现宿主机之外的网络访问</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps  | grep centos6_sshd</span></span><br><span class="line">CONTAINER ID   IMAGE        COMMAND           CREATED             STATUS       PORTS     NAMES</span><br><span class="line">38990dc22fec   f1b33ecfb107  &quot;/usr/sbin/sshd -D&quot; 6 seconds ago    Up 5 seconds           centos6_ssh_d</span><br><span class="line">d6f755ad89cf   f1b33ecfb107   &quot;/bin/bash&quot;        13 minutes ago   Up 13 minutes          centos6_sshd</span><br></pre></td></tr></table></figure>

<h4 id="制作centos7的ssh服务镜像"><a href="#制作centos7的ssh服务镜像" class="headerlink" title="制作centos7的ssh服务镜像"></a>制作centos7的ssh服务镜像</h4><ul>
<li>在centos7.6容器中配置yum源并安装ssh服务</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -it --name centos7_sshd f1cb7c7d58b7</span></span><br><span class="line">d4b3c440209458a303df244018588d936942460ab55bc50ada9f4a70b9869e7d</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">将7的yum源配置到centos7.6容器中</span></span><br><span class="line">[root@9d4b3c4402094 /]# cat &gt; /etc/yum.repos.d/ftp7.repo &lt;&lt; EOF</span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">[ftp7.6]</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">name=ftp7.6</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">baseurl=ftp://172.17.0.1/centos7.6</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">enabled=1</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">gpgcheck=0</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">EOF</span></span><br><span class="line"></span><br><span class="line">[root@d4b3c4402094 /]# yum clean all &amp;&amp; yum repolist</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装ssh服务并启动，以便通过ssh服务进行登录容器</span></span><br><span class="line">[root@d4b3c4402094 /]# yum -y install openssh* net-tools</span><br><span class="line">[root@d4b3c4402094 ~]# ifconfig  | grep 172.17</span><br><span class="line">    inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">[root@d4b3c4402094 /]# yum -y install openssh*</span><br><span class="line">[root@d4b3c4402094 /]# systemctl start sshd         # 使用该命令启动不了ssh服务</span><br><span class="line">Failed to get D-Bus connection: Operation not permitted</span><br><span class="line"></span><br><span class="line">[root@d4b3c4402094 /]# /etc/init.d/sshd start        # 该脚本启动也有很多命令没有</span><br><span class="line">/etc/init.d/sshd: line 30: /etc/rc.d/init.d/functions: No such file or directory</span><br><span class="line">/usr/sbin/sshd-keygen: line 10: /etc/rc.d/init.d/functions: No such file or directory</span><br><span class="line">Generating SSH2 RSA host key: /usr/sbin/sshd-keygen: line 63: success: command not found</span><br><span class="line"></span><br><span class="line">Generating SSH2 ECDSA host key: /usr/sbin/sshd-keygen: line 105: success: command not found</span><br><span class="line"></span><br><span class="line">Generating SSH2 ED25519 host key: /usr/sbin/sshd-keygen: line 126: success: command not found</span><br><span class="line"></span><br><span class="line">Starting sshd: /etc/init.d/sshd: line 64: success: command not found</span><br><span class="line">/etc/init.d/sshd: line 64: failure: command not found</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">手动初始化sshd服务</span></span><br><span class="line">[root@d4b3c4402094 /]# mkdir /var/run/sshd</span><br><span class="line">[root@d4b3c4402094 /]# echo &#x27;UseDNS no&#x27; &gt;&gt; /etc/ssh/sshd_config</span><br><span class="line">[root@d4b3c4402094 /]# sed -i -e &#x27;/pam_loginuid.so/d&#x27; /etc/pam.d/sshd</span><br><span class="line">[root@d4b3c4402094 /]# echo &#x27;root:123456&#x27; | chpasswd</span><br><span class="line"></span><br><span class="line">[root@d4b3c4402094 /]# /usr/bin/ssh-keygen -A  # 初始化成功</span><br><span class="line">ssh-keygen: generating new host keys: RSA1 DSA </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">保存含有ssh服务的镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container <span class="built_in">ls</span> |grep centos7_sshd</span></span><br><span class="line">CONTAINER ID   IMAGE   COMMAND    CREATED    STATUS    PORTS   NAMES</span><br><span class="line">d4b3c4402094   f1cb7c7d58b7  &quot;/bin/bash&quot;  7 minutes ago   Up 7 minutes    centos7_sshd</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker commit -m <span class="string">&quot;centos7 add sshd&quot;</span> d4b3c4402094 scaven/centos:sshd_v1</span></span><br><span class="line">sha256:40b6e896f747f9ea5757ff36f1803e1d8e7a16189a53838b2258e36ff0da3ce4</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动含有ssh服务的镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d --name testsshd  -p 222:22 40b6e896f747 /usr/sbin/sshd -D</span></span><br><span class="line">03a9e7608154a856877fa13479b0dace6eb8c02bc14a3a2654e4a5c62d72c18f</span><br></pre></td></tr></table></figure>

<p>通过xshell连接</p>
<p>![image-20200720004135508]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720004135508.png)</p>
<h3 id="构建企业网站BBS镜像"><a href="#构建企业网站BBS镜像" class="headerlink" title="构建企业网站BBS镜像"></a>构建企业网站BBS镜像</h3><h4 id="制作lamp环境"><a href="#制作lamp环境" class="headerlink" title="制作lamp环境"></a>制作lamp环境</h4><ul>
<li>基于centos6启动容器并配置yum源</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在宿主机上新建目录存放MySQL和web网页目录，以便做持久化存储</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">mkdir</span> -p /opt/Volume/bbs/mysql /opt/Volume/bbs/html</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -it -d --name centos6_bbs -v /opt/Volume/bbs/mysql/:/var/lib/mysql -v /opt/Volume/bbs/html/:/var/www/html  48650444e419</span></span><br><span class="line">b8d9eb05d4ed43a192a850c0c0792d1e0b0cb1c0d95c3d084f1785fd2e5ac0a3</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">配置yum源</span></span><br><span class="line">[root@b8d9eb05d4ed /]# cat &gt; /etc/yum.repos.d/ftp6.repo &lt;&lt; EOF</span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">[ftp6_10]</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">name=ftp6_10</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">baseurl=ftp://172.17.0.1/centos6.10</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">enabled=1</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">gpgcheck=0</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash">EOF</span></span><br><span class="line"></span><br><span class="line">[root@b8d9eb05d4ed /]# yum clean all &amp;&amp; yum repolist</span><br></pre></td></tr></table></figure>

<ul>
<li>在容器内安装lamp</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装lamp</span></span><br><span class="line">[root@b8d9eb05d4ed /]# yum -y install openssh* httpd mysql php php-mysql mysql-server</span><br></pre></td></tr></table></figure>

<ul>
<li>初始化ssh服务</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">ssh服务初始化</span><br><span class="line">[root@b8d9eb05d4ed /]# /etc/init.d/sshd start</span><br><span class="line">Generating SSH2 RSA host key:                              [  OK  ]</span><br><span class="line">Generating SSH1 RSA host key:                              [  OK  ]</span><br><span class="line">Generating SSH2 DSA host key:                              [  OK  ]</span><br><span class="line">Starting sshd:                                             [  OK  ]</span><br><span class="line">[root@b8d9eb05d4ed /]# echo &quot;root:123456&quot; | chpasswd </span><br></pre></td></tr></table></figure>

<ul>
<li>在容器内初始化mysql服务</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">[root@b8d9eb05d4ed /]# /etc/init.d/mysqld  start</span><br><span class="line">Initializing MySQL database:  Installing MySQL system tables...</span><br><span class="line">OK</span><br><span class="line">Filling help tables...</span><br><span class="line">OK</span><br><span class="line">···</span><br><span class="line">Please report any problems with the /usr/bin/mysqlbug script!  [  OK  ]</span><br><span class="line">Starting mysqld:                                               [  OK  ]</span><br><span class="line"></span><br><span class="line">[root@b8d9eb05d4ed /]# cd /var/lib/mysql/</span><br><span class="line">[root@b8d9eb05d4ed mysql]# ll </span><br><span class="line">total 20484</span><br><span class="line">-rw-rw---- 1 mysql mysql  5242880 Jul 19 14:59 ib_logfile0</span><br><span class="line">-rw-rw---- 1 mysql mysql  5242880 Jul 19 14:59 ib_logfile1</span><br><span class="line">-rw-rw---- 1 mysql mysql 10485760 Jul 19 14:59 ibdata1</span><br><span class="line">drwx------ 2 mysql mysql     4096 Jul 19 14:59 mysql</span><br><span class="line">srwxrwxrwx 1 mysql mysql        0 Jul 19 14:59 mysql.sock</span><br><span class="line">drwx------ 2 mysql mysql        6 Jul 19 14:59 test</span><br><span class="line"></span><br><span class="line">[root@b8d9eb05d4ed mysql]# mysql  # 启动mysql</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">新建数据库、用户和授权</span></span><br><span class="line"><span class="meta prompt_">mysql&gt; </span><span class="language-bash">grant all on *.* to  root@<span class="string">&#x27;localhost&#x27;</span> identified by <span class="string">&#x27;123456&#x27;</span>;</span></span><br><span class="line">Query OK, 0 rows affected (0.00 sec)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">mysql&gt; </span><span class="language-bash">grant all on *.* to  discuz@<span class="string">&#x27;%&#x27;</span> identified by <span class="string">&#x27;123456&#x27;</span>;</span></span><br><span class="line">Query OK, 0 rows affected (0.00 sec)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">mysql&gt; </span><span class="language-bash">grant all on *.* to  discuz@<span class="string">&#x27;localhost&#x27;</span> identified by <span class="string">&#x27;123456&#x27;</span>;</span></span><br><span class="line">Query OK, 0 rows affected (0.00 sec)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">mysql&gt; </span><span class="language-bash">create database discuz charset utf8;</span>  </span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">检查宿主机上文件持久化存储结果</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">ls</span> /opt/Volume/bbs/mysql/</span></span><br><span class="line">ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  test</span><br></pre></td></tr></table></figure>

<ul>
<li>在容器内初始化http服务</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@b8d9eb05d4ed mysql]# /etc/init.d/httpd  start</span><br><span class="line">Starting httpd: httpd: Could not reliably determine the server&#x27;s fully qualified domain name, using 172.17.0.6 for ServerName                                     [  OK  ]</span><br></pre></td></tr></table></figure>

<ul>
<li>通过宿主机访问nginx容器</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">curl -I 172.17.0.6</span></span><br><span class="line">HTTP/1.1 403 Forbidden</span><br><span class="line">Date: Sun, 19 Jul 2020 15:09:08 GMT</span><br><span class="line">Server: Apache/2.2.15 (CentOS)</span><br><span class="line">Accept-Ranges: bytes</span><br><span class="line">Content-Length: 4961</span><br><span class="line">Connection: close</span><br><span class="line">Content-Type: text/html; charset=UTF-8</span><br></pre></td></tr></table></figure>

<ul>
<li>保存镜像</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker commit b8d9eb05d4ed centos6_lamp:v1</span></span><br><span class="line">sha256:f022bb774837bd1f28646ca0222005e6962e98a39f7d7ce50ffedd5dbb82a758</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span> <span class="comment"># grep lamp</span></span></span><br><span class="line">REPOSITORY    TAG     IMAGE ID        CREATED            SIZE</span><br><span class="line">centos6_lamp   v1     f022bb774837   6 seconds ago       476MB</span><br></pre></td></tr></table></figure>

<ul>
<li>测试lamp服务</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">停止第一版容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container stop centos6_bbs</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用lamp第一版镜像启动容器，并暴露80端口</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -it --name centos6_lamp -p 8080:80 -v /opt/Volume/bbs/mysql/:/var/lib/mysql -v /opt/Volume/bbs/html/:/var/www/html f022bb774837</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动MySQL和http服务</span></span><br><span class="line">[root@e6551f8c73eb /]# /etc/init.d/mysqld start</span><br><span class="line">Starting mysqld:                                           [  OK  ]</span><br><span class="line">[root@e6551f8c73eb /]# /etc/init.d/httpd start</span><br><span class="line">Starting httpd: httpd: Could not reliably determine the server&#x27;s fully qualified domain name, using 172.17.0.6 for ServerName                                  [  OK  ]</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">编辑简单的php页面php和http访问情况</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">cat</span>  /opt/Volume/bbs/html/index.php  <span class="comment"># 在宿主机上编写即可</span></span></span><br><span class="line">&lt;?php</span><br><span class="line">    phpinfo();</span><br><span class="line">?&gt;</span><br></pre></td></tr></table></figure>

<p>此时，可通过浏览器测试php的访问状态：</p>
<p>![image-20200719232205533]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719232205533.png)</p>
<h4 id="安装bbs"><a href="#安装bbs" class="headerlink" title="安装bbs"></a>安装bbs</h4><ul>
<li>下载bbs软件包</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">git <span class="built_in">clone</span> https://gitee.com/ComsenzDiscuz/DiscuzX.git</span></span><br><span class="line">Cloning into &#x27;DiscuzX&#x27;...</span><br><span class="line">remote: Enumerating objects: 3493, done.</span><br><span class="line">remote: Counting objects: 100% (3493/3493), done.</span><br><span class="line">remote: Compressing objects: 100% (905/905), done.</span><br><span class="line">remote: Total 11627 (delta 2935), reused 2670 (delta 2588), pack-reused 8134</span><br><span class="line">Receiving objects: 100% (11627/11627), 14.85 MiB | 5.71 MiB/s, done.</span><br><span class="line">Resolving deltas: 100% (5734/5734), done.</span><br></pre></td></tr></table></figure>

<ul>
<li>将bb软件移动至http服务根目录</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">cd</span> DiscuzX/</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">ls</span></span> </span><br><span class="line">readme  README.md  upload  utility</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">将upload目录下的所有文件拷贝到http服务根目录下，并给与777的权限</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">cp</span> -r  upload/*  /opt/Volume/bbs/html/</span></span><br><span class="line">cp: overwrite ‘/opt/Volume/bbs/html/index.php’? yes</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">chmod</span> -R 777 /opt/Volume/bbs/html/</span></span><br></pre></td></tr></table></figure>

<ul>
<li>浏览器访问并安装bbs</li>
</ul>
<p>接下来就是页面化操作了</p>
<p>&lt;img src&#x3D;&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719233021172.png” alt&#x3D;”image-20200719233021172” style&#x3D;”zoom:50%;” &#x2F;&gt;</p>
<p>检查状态是否符合要求</p>
<p>&lt;img src&#x3D;&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719233137145.png” alt&#x3D;”image-20200719233137145” style&#x3D;”zoom:50%;” &#x2F;&gt;</p>
<p>设置运行环境</p>
<p>&lt;img src&#x3D;&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719233215227.png” alt&#x3D;”image-20200719233215227” style&#x3D;”zoom:50%;” &#x2F;&gt;</p>
<p>安装数据库，设置数据库信息</p>
<p>&lt;img src&#x3D;&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719233803196.png” alt&#x3D;”image-20200719233803196” style&#x3D;”zoom:50%;” &#x2F;&gt;</p>
<p>安装完成，点击右下角进入论坛</p>
<p>&lt;img src&#x3D;&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719233927216.png” alt&#x3D;”image-20200719233927216” style&#x3D;”zoom:50%;” &#x2F;&gt;</p>
<p>进入论坛，结果正常</p>
<p>![image-20200719233956493]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200719233956493.png)</p>
<ul>
<li>保存第二版镜像</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker commit -m <span class="string">&quot;welcom to bbs&quot;</span>  e6551f8c73eb scaven/centos6_bbs:v1</span></span><br><span class="line">sha256:5259a77d042b9f0a2ca73f385603733d025b22c1ce3e549e1c25f95cd7c8bdcb</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span></span></span><br><span class="line">REPOSITORY           TAG      IMAGE ID     CREATED        SIZE</span><br><span class="line">scaven/centos6_bbs   v1       5259a77d042b 10 seconds ago 477MB</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">停止容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container stop e6551f8c73eb</span></span><br></pre></td></tr></table></figure>

<h4 id="创建启动脚本"><a href="#创建启动脚本" class="headerlink" title="创建启动脚本"></a>创建启动脚本</h4><p>docker容器在启动时只能指定启动一个进程，那么，我们可以编写一个启动脚本，只要把需要启动的服务放在该脚本中，并最后保持运行该脚本的窗口不退出即可。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">cat</span>  /opt/Volume/bbs/html/init.sh</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">/etc/init.d/mysqld start   # 启动MySQL</span><br><span class="line">/etc/init.d/httpd start    # 启动http</span><br><span class="line">/usr/sbin/sshd -D          # 启动ssh，该命令是前端运行，即端口不退出</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">chmod</span> +x  /opt/Volume/bbs/html/init.sh</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">基于第二版镜像，启动容器，暴露http端口，MySQL端口，ssh端口，并执行上面创建的脚本</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d --name centos6_bbs_v1 -p 8000:80 -p 2222:22 -p 13306:3306 -v /opt/Volume/bbs/mysql/:/var/lib/mysql -v /opt/Volume/bbs/html/:/var/www/html 5259a77d042b /var/www/html/init.sh</span></span><br><span class="line">97bf03282d31439ed03201e95aaba52443c86718fc9f5fd0772b2884c00af7c3</span><br></pre></td></tr></table></figure>

<p>检查验证–ssh服务</p>
<ul>
<li>宿主机连接ssh服务，通过ip地址直接连接就可以，不需要使用端口</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">ssh 172.17.0.6</span></span><br><span class="line">root@172.17.0.6&#x27;s password:  # 输入密码 </span><br><span class="line">Last login: Sun Jul 19 16:12:52 2020 from 10.138.3.1</span><br><span class="line">[root@97bf03282d31 ~] # </span><br></pre></td></tr></table></figure>

<ul>
<li>外部网络通过xshell连接容器</li>
</ul>
<p>![image-20200720001324660]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720001324660.png)</p>
<p>检查验证–bbs论坛</p>
<p>![image-20200720000946358]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720000946358.png)</p>
<p>检查验证–mysql服务</p>
<p>&lt;img src&#x3D;&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720001520071.png” alt&#x3D;”image-20200720001520071” style&#x3D;”zoom:50%;” &#x2F;&gt;</p>
<h2 id="通过编制dockerfile文件制作docker镜像"><a href="#通过编制dockerfile文件制作docker镜像" class="headerlink" title="通过编制dockerfile文件制作docker镜像"></a>通过编制dockerfile文件制作docker镜像</h2><h3 id="dockerfile文件常用指令"><a href="#dockerfile文件常用指令" class="headerlink" title="dockerfile文件常用指令"></a>dockerfile文件常用指令</h3><ul>
<li><code>FROM</code>：指定基础镜像</li>
</ul>
<p>格式: <code>FROM &lt;image&gt; 或者 FROM &lt;image&gt;:&lt;tag&gt;</code></p>
<p>FROM指令的功能是为后面的指令提供基础镜像，因此Dockerfile必须以FROM指令作为第一条非注释指令。从公共镜像库中拉取镜像很容易，基础镜像可以选择任何有效的镜像。在一个Dockerfile中FROM指令可以出现多次，这样会构建多个镜像。tag的默认值是latest，如果参数image或者tag指定的镜像不存在，则返回错误。</p>
<ul>
<li><code>LABLE</code>：用于指定作者或一些跟作者相关信息</li>
</ul>
<p>格式为：LABLE   user</p>
<p>案例：LABLE “username usermail”</p>
<ul>
<li><code>ENV</code>：声明变量</li>
</ul>
<p>格式: <code>ENV &lt;key&gt; &lt;value&gt; 或者 ENV &lt;key&gt;=&lt;value&gt; ...</code></p>
<p>ENV指令可以为镜像创建出来的容器声明环境变量。并且在Dockerfile中，ENV指令声明的环境变量可以被后面的特定指令(即ENV、ADD、COPY、WORKDIR、EXPOSE、VOLUME、USER)解释使用。其他指令使用环境变量时，使用格式为<code>$variable_name</code>或者<code>$&#123;variable_name&#125;</code>。如果在变量面前添加斜杠\可以转义。如<code>\$foo</code>或者<code>\$&#123;foo&#125;</code>将会被转换为<code>$foo</code>和<code>$&#123;foo&#125;</code>,而不是环境变量所保存的值。</p>
<ul>
<li><code>COPY</code>：复制本地文件或目录到镜像中</li>
</ul>
<p>格式: <code>COPY &lt;src&gt; &lt;dest&gt;</code></p>
<p>COPY指令复制所指向的文件或目录,将它添加到新镜像中,复制的文件或目录在镜像中的路径是<code>&lt;dest&gt;</code>。<code>&lt;src&gt;</code>所指定的源可以有多个,但必须是上下文根目录中的相对路径。此外,<code>&lt;src&gt;</code>可以使用通配符指向所有匹配通配符的文件或目录，例如，<code>COPY home* /mydir/ </code>表示添加所有以”hom”开头的文件到目录&#x2F;mydir&#x2F;中。</p>
<p><code>&lt;dest&gt;</code>可以是文件或目录，但必须是目标镜像中的绝对路径或者相对于WORKDIR的相对路径(WORKDIR即Dockerfile中WORKDIR指令指定的路径,用来为其他指令设置工作目录)。<br>​		若<code>&lt;dest&gt;</code>以反斜杠&#x2F;结尾则其指向的是目录；否则指向文件。<code>&lt;src&gt;</code>同理。若<code>&lt;dest&gt;</code>是一个文件，则<code>&lt;src&gt;</code>的内容会被写到<code>&lt;dest&gt;</code>中；否则<code>&lt;src&gt;</code>指向的文件或目录中的内容会被复制添加到<code>&lt;dest&gt;</code>目录中。<br>​		当<code>&lt;src&gt;</code>指定多个源时，<code>&lt;dest&gt;</code>必须是目录。如果<code>&lt;dest&gt;</code>不存在，则路径中不存在的目录会被创建。</p>
<ul>
<li><code>ADD</code>：复制宿主机或网络文件到镜像中</li>
</ul>
<p>格式：<code>ADD &lt;src&gt; &lt;dest&gt;</code></p>
<p>ADD与COPY指令在功能上很相似，都支持复制本地文件到镜像的功能，但ADD指令还支持其他功能，如解压和下载网络文件。<code>&lt;src&gt;</code>可以是指向网络文件的URL,此时若<code>&lt;dest&gt;</code>指向一个目录，则URL必须是完全路径，这样可以获得网络文件的文件名filename，该文件会被复制添加到<code>&lt;dest&gt;/&lt;filename&gt;</code>。<code>&lt;src&gt;</code>还可以指向一个本地压缩归档文件，该文件会在复制到容器时会被解压提取，如ADD sxample.tar.xz &#x2F;。但是若URL中的文件为归档文件则不会被解压提取。ADD 和 COPY指令虽然功能相似，但一般推荐使用COPY，因为COPY只支持本地文件，相比ADD而言，它更加透明。</p>
<ul>
<li><code>EXPOSE</code>：暴露端口</li>
</ul>
<p>格式: <code>EXPOSE &lt;port&gt; [&lt;port&gt;/&lt;protocol&gt;...]</code></p>
<p>EXPOSE指令通知Docker该容器在运行时侦听指定的网络端口。可以指定端口是侦听TCP还是UDP，如果未指定协议，则默认值为TCP。这个指令仅仅是声明容器打算使用什么端口而已，并不会自动在宿主机进行端口映射,可以在运行容器时通过<code> -p</code>指定。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">EXPOSE 80/tcp</span><br><span class="line">EXPOSE 80/udp</span><br></pre></td></tr></table></figure>

<ul>
<li><code>USER</code>：指定运行用户</li>
</ul>
<p>格式: <code>USER &lt;user&gt;[:&lt;group] 或者 USER &lt;UID&gt;[:&lt;GID&gt;]</code></p>
<p>USER指令设置了user name和user group(可选)。在它之后的RUN、CMD以及ENTRYPOINT指令都会以设置的user来执行。</p>
<ul>
<li><code>VOLUME</code> ：设置容器的挂载卷</li>
</ul>
<p>格式：<code>VOLUME [&quot;&lt;路径1&gt;&quot;, &quot;&lt;路径2&gt;&quot;...]</code><br>          <code>VOLUME &lt;路径&gt;</code></p>
<p>Docker的使用原则除了每个容器干尽量少的事情外，还尽可能要求容器运行时应该尽量保持容器存储层不发生写操作，对于数据库类需要保存动态数据的应用，其数据库文件应该保存于卷(volume)中，也就是将本地磁盘的某一个目录挂载至容器内。同时这样的共享目录可以被多个不同的容器所使用。如果在容器启动时指定了数据卷，那么dockerfile中定义的挂在卷将被覆盖。</p>
<ul>
<li><code>ARG</code>：构建参数</li>
</ul>
<p>格式：<code> ARG &lt;name&gt;[=&lt;default value&gt;]</code></p>
<p>ARG和ENV效果类似，都是用来设置环境变量的。唯一不同的是dockerfile中的ARG编译好后是不会出现在打开的容器内的。ARG在dockerfile中创建一个全局参数，参数可以给定一个默认值，在编译时可以传参对其进行覆盖。如果ARG指令有默认值并且在构建期间没有接收到参数、则使用默认值。一个dockerfile中可以包含多个ARG参数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build --build-arg &lt;varname&gt;=&lt;value&gt;</span><br></pre></td></tr></table></figure>

<p>可以使用ARG或ENV指令来指定RUN指令可用的变量，如果ARG和ENV同时指定了一个相同名称的变量、则ENV设置的变量会覆盖ARG设置的变量。如下:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1 FROM ubuntu</span><br><span class="line">2 ARG CONT_IMG_VER</span><br><span class="line">3 ENV CONT_IMG_VER v1.0.0</span><br><span class="line">4 RUN echo $CONT_IMG_VER</span><br></pre></td></tr></table></figure>

<p>​		使用 docker build –build-arg CONT_IMG_VER&#x3D;v2.0.1 . 最终输出v1.0.0 。一个ARG指令的有效范围在其定义的构建阶段内、如果要在多个阶段中都有效、则必须在每个阶段都使用ARG指令；与ARG不同 ENV设置参数的有效期为整个构建期内。</p>
<ul>
<li><code>WORKDIR</code>：指定工作目录</li>
</ul>
<p>格式: <code>WORKDIR /path/to/workdir</code></p>
<p>WORKDIR指令设置工作目录，它之后的RUN、CMD、ENTRYPOINT、COPY以及ADD指令都会在这个工作目录下运行。如果这个工作目录不存在，则会自动创建一个。WORKDIR指令可在Dockerfile中多次使用。如果提供了相对路径，则它将相对于上一个WORKDIR指令的路径。例如</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">WORKDIR /a</span><br><span class="line">WORKDIR b</span><br><span class="line">WORKDIR c</span><br><span class="line">RUN pwd</span><br></pre></td></tr></table></figure>

<p>输出结果是 &#x2F;a&#x2F;b&#x2F;c</p>
<ul>
<li><code>RUN</code>：指定运行的命令</li>
</ul>
<p>格式1： <code>RUN &lt;command&gt;</code> (shell格式)<br>格式2： <code>RUN [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;]</code> (exec格式，推荐使用)</p>
<p>RUN指令会在前一条命令创建出的镜像的基础上创建一个容器，并在容器中运行命令，在命令结束运行后提交容器为新镜像，新镜像被Dockerfile中的下一条指令使用。RUN指令的两种格式表示命令在容器中的两种运行方式<strong>。当使用shell格式时，命令通过&#x2F;bin&#x2F;sh -c运行。当使用exec格式时，命令是直接运行的，容器不调用shell程序，即容器中没有shell程序</strong>。<strong>exec格式中的参数会被当成JSON数组被Docker解析，故必须使用双引号而不能使用单引号。因为exec格式不会在shell中执行，所以环境变量的参数不会被替换</strong>。</p>
<p>比如执行<code>RUN [&quot;echo&quot;, &quot;$HOME&quot;]</code>指令时，$HOME不会做变量替换。如果希望运行shell程序，指令可以写成 <code>RUN [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;echo&quot;, &quot;$HOME&quot;]</code>。</p>
<ul>
<li><code>CMD</code>：指定容器启动时运行的命令</li>
</ul>
<p>CMD指令有3种格式。</p>
<p>格式1：<code>CMD &lt;command&gt;</code> (shell格式)<br>格式2：<code>CMD [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;]</code> (exec格式，推荐使用)<br>格式3：<code>CMD [&quot;param1&quot;, &quot;param2&quot;]</code> (为ENTRYPOINT指令提供参数)</p>
<p>CMD指令提供容器运行时的默认值，这些默认值可以是一条指令，也可以是一些参数。<strong>一个Dockerfile中可以有多条CMD指令，但只有最后一条CMD指令有效</strong>。<br>​		CMD [“param1”, “param2”]格式是在CMD指令和ENTRYPOINT指令配合时使用的，CMD指令中的参数会添加到ENTRYPOING指令中，<strong>使用shell和exec格式时，命令在容器中的运行方式与RUN指令相同</strong>。<strong>不同之处在于，RUN指令在构建镜像时执行命令，并生成新的镜像；CMD指令在构建镜像时并不执行任何命令，而是在容器启动时默认将CMD指令作为第一条执行的命令</strong>。<strong>如果用户在命令行界面运行docker run命令时指定了命令参数，则会覆盖CMD指令中的命令。</strong></p>
<ul>
<li><code>ENTRYPOINT</code>：指定容器启动时运行的命令</li>
</ul>
<p>ENTRYPOINT指令有两种格式。</p>
<p>格式1：<code>ENTRYPOINT &lt;command&gt;</code> (shell格式)<br>格式2：<code>ENTRYPOINT [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;]</code> (exec格式，推荐格式)</p>
<p>ENTRYPOINT指令和CMD指令类似，都可以让容器在每次启动时执行相同的命令，但它们之间又有不同。<strong>一个Dockerfile中可以有多条ENTRYPOINT指令，但只有最后一条ENTRYPOINT指令有效</strong>。<strong>当使用Shell格式时，ENTRYPOINT指令会忽略任何CMD指令和docker run命令的参数，并且会运行在bin&#x2F;sh -c中。这意味着ENTRYPOINT指令进程为bin&#x2F;sh -c的子进程,进程在容器中的PID将不是1，且不能接受Unix信号</strong>。<strong>即当使用<code>docker stop &lt;container&gt;</code>命令时，命令进程接收不到SIGTERM信号</strong>。推荐使用exec格式，使用此格式时，docker run传入的命令参数会覆盖CMD指令的内容并且附加到ENTRYPOINT指令的参数中。从ENTRYPOINT的使用中可以看出，CMD可以是参数，也可以是指令，而ENTRYPOINT只能是命令；另外，<strong>docker run命令提供的运行命令参数可以覆盖CMD,但不能覆盖ENTRYPOINT</strong>。</p>
<h3 id="制作ssh服务的centos6镜像"><a href="#制作ssh服务的centos6镜像" class="headerlink" title="制作ssh服务的centos6镜像"></a>制作ssh服务的centos6镜像</h3><p>格式：</p>
<ol>
<li>FROM：指定该镜像基于的基础镜像<ul>
<li>格式1：<code>FROM centos:centos6.10</code>  指定从那个仓库中的那个镜像，不建议使用，tag是很容易进行人为修改的，因此不安全</li>
<li>格式2： FROM centos@镜像id    建议使用该种方法</li>
</ul>
</li>
<li>RUN：指定需要运行的命令<ul>
<li>格式1：常规shell命令：<code>RUN +shell命令</code>，值得注意的是，命令必须存在</li>
<li>格式2：非shell命令：<code>RUN [&quot;命令&quot;,&quot;参数或选项1&quot;,&quot;参数或选项2&quot;,&quot;····&quot;]</code>，如 <code>RUN [&quot;mysqld&quot;,&quot;--datadir=xx&quot;,&quot;--basedir=xx&quot;,&quot;--user=mysql&quot;]</code></li>
</ul>
</li>
<li>CMD：容器启动时执行的命令，一般是需要能夯住在前端的，不退出窗口的命令<ul>
<li>格式：<code>CMD [&quot;命令&quot;,&quot;参数或选项1&quot;,&quot;参数或选项2&quot;,&quot;····&quot;]</code></li>
</ul>
</li>
<li>EXPOSE：指定该容器暴露的端口</li>
</ol>
<h4 id="编写Dockerfile文件"><a href="#编写Dockerfile文件" class="headerlink" title="编写Dockerfile文件"></a>编写Dockerfile文件</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">删除上面实验的所有容器</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container <span class="built_in">rm</span> -f `docker container <span class="built_in">ls</span> -a -q`</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="built_in">mkdir</span> -p /data/dockerfile; <span class="built_in">cd</span>  /data/dockerfile  <span class="comment"># 创建制作镜像目录</span></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">开始制作镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">vim Dockerfile  <span class="comment"># 新建该文件，名称可以是小写</span></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">备注信息：Centos6.10_SSHDv1.0</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1 设置基础镜像，建议使用下面的写法，而不是使用tag标签</span></span><br><span class="line">FROM centos:centos6.10</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">2 添加制作镜像的相关命令，建议多个命令一起写，不然层级太多</span></span><br><span class="line">RUN mv /etc/yum.repos.d/*.repo /tmp &amp;&amp; echo -e &quot;[ftp6]\nname=ftp6\nbaseurl=ftp://172.17.0.1/centos6.10\nenabled=1\ngpgcheck=0&quot; &gt; /etc/yum.repos.d/ftp6.repo  &amp;&amp; yum clean all &amp;&amp; yum repolist &amp;&amp; yum -y install openssh-server </span><br><span class="line"></span><br><span class="line">RUN /etc/init.d/sshd start &amp;&amp; echo &quot;root:123456&quot; | chpasswd</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">3 容器启动时指定的命令</span></span><br><span class="line">CMD [&quot;/usr/sbin/sshd&quot;,&quot;-D&quot;]</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">4 暴露端口</span></span><br><span class="line">EXPOSE 22</span><br></pre></td></tr></table></figure>

<h4 id="使用Dockerfile文件生成镜像"><a href="#使用Dockerfile文件生成镜像" class="headerlink" title="使用Dockerfile文件生成镜像"></a>使用Dockerfile文件生成镜像</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">制作镜像，默认就会使用当前目录中的Dockerfile文件</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image build -t <span class="string">&quot;centos6_10_add_sshd&quot;</span> ./</span></span><br><span class="line">Sending build context to Docker daemon   2.56kB</span><br><span class="line">Step 1/5 : FROM centos:centos6.10  # 第一步完成后生成的镜像</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">48650444e419</span></span><br><span class="line">Step 2/5 : RUN mv /etc/yum.repos.d/*.repo /tmp &amp;&amp; echo -e &quot;[ftp6]\nname=ftp6\nbaseurl=ftp://172.17.0.1/centos6.10\nenabled=1\ngpgcheck=0&quot; &gt; /etc/yum.repos.d/ftp6.repo  &amp;&amp; yum clean all &amp;&amp; yum repolist &amp;&amp; yum -y install openssh-server</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Using cache</span></span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">4402742653b8   <span class="comment"># 第二步完成后生成的镜像</span></span></span><br><span class="line">Step 3/5 : RUN /etc/init.d/sshd start &amp;&amp; /etc/init.d/sshd stop &amp;&amp; echo &quot;root:123456&quot; | chpasswd</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> 41576869d2c5</span></span><br><span class="line">Generating SSH2 RSA host key: [  OK  ]</span><br><span class="line">Generating SSH1 RSA host key: [  OK  ]</span><br><span class="line">Generating SSH2 DSA host key: [  OK  ]</span><br><span class="line">Starting sshd: [  OK  ]</span><br><span class="line">Stopping sshd: [  OK  ]</span><br><span class="line">Removing intermediate container 41576869d2c5</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">f84b609cb2a8  <span class="comment"># 第三步完成后生成的镜像</span></span></span><br><span class="line">Step 4/5 : CMD [&quot;/usr/sbin/sshd&quot;,&quot;-D&quot;]</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> 453a0196b369</span></span><br><span class="line">Removing intermediate container 453a0196b369</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">fd7d7bdbd6eb   <span class="comment"># 第四步完成后生成的镜像</span></span></span><br><span class="line">Step 5/5 : EXPOSE 22</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> 029d6942a429</span></span><br><span class="line">Removing intermediate container 029d6942a429</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">0545dad565d5   <span class="comment"># 第五步完成后生成的镜像</span></span></span><br><span class="line">Successfully built 0545dad565d5</span><br><span class="line">Successfully tagged centos6_10_add_sshd:latest</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">上面的每一步都可以使用<span class="string">&quot;docker run -it --rm 镜像id /bin/bash&quot;</span>进入容器内进行检查和调试</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">制作完成后，在宿主机上查看镜像</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker image <span class="built_in">ls</span> | grep centos6_10_add_sshd</span></span><br><span class="line">centos6_10_add_sshd  latest  0545dad565d5   About a minute ago   281MB</span><br></pre></td></tr></table></figure>

<h4 id="验证镜像"><a href="#验证镜像" class="headerlink" title="验证镜像"></a>验证镜像</h4><p>启动镜像并通过ssh远程登陆。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用刚制作的镜像启动容器，检查ssh服务和是否后端运行</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -it -d  --name ssh -p 222:22 0545dad565d5</span> </span><br><span class="line">e14f92e87ef27406e95001b66c2cd0736fcb181d3ec586bcebee62f24c43ce57</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看容器ip地址，在本机上可通过容器IP地址+容器端口登录</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container inspect e14f92e87ef2 | grep IPAddress</span></span><br><span class="line">            &quot;SecondaryIPAddresses&quot;: null,</span><br><span class="line">            &quot;IPAddress&quot;: &quot;172.17.0.2&quot;,</span><br><span class="line">                    &quot;IPAddress&quot;: &quot;172.17.0.2&quot;,</span><br></pre></td></tr></table></figure>

<p>使用xshell远程登录</p>
<p>![image-20200720013745449]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720013745449.png)</p>
<h3 id="制作lamp镜像"><a href="#制作lamp镜像" class="headerlink" title="制作lamp镜像"></a>制作lamp镜像</h3><p>其他常用命令格式：</p>
<ol>
<li><p><code>COPY</code>：复制本地文件到容器指定目录下，<code>支持通配符</code>；如果拷贝的是一个目录，则只拷贝目录下的文件(将需要拷贝的文件放在dockefile同目录中)。</p>
<ul>
<li>格式：COPY  &lt;src1,src2,·····&gt; <dest></li>
</ul>
</li>
<li><p><code>ADD</code>：跟COPY一样，多了一个功能是能自动解压<code>.tar</code>的文件，并且还可以指定源文件为url地址，即可以进行网络下载</p>
</li>
</ol>
<h4 id="编制dockerfile文件"><a href="#编制dockerfile文件" class="headerlink" title="编制dockerfile文件"></a>编制dockerfile文件</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">[root@docker bbs]# ls </span><br><span class="line">DiscuzX.tar  dockerfile  init.sh</span><br><span class="line">[root@docker bbs]# cat dockerfile </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Centos6.10_SSHD_LAMP</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1 设置基础镜像，建议使用下面的写法，而不是使用tag</span></span><br><span class="line">FROM centos:centos6.10</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">2 添加需要执行的程序等</span></span><br><span class="line">RUN mv /etc/yum.repos.d/*.repo /tmp &amp;&amp; echo -e &quot;[ftp6]\nname=ftp6\nbaseurl=ftp://172.17.0.1/centos6.10\nenabled=1\ngpgcheck=0&quot; &gt; /etc/yum.repos.d/ftp6.repo  &amp;&amp; yum clean all &amp;&amp; yum repolist &amp;&amp; yum -y install openssh-server mysql mysql-server php php-mysql httpd</span><br><span class="line"></span><br><span class="line">RUN /etc/init.d/sshd start &amp;&amp; /etc/init.d/sshd stop &amp;&amp; echo &quot;root:123456&quot; | chpasswd &amp;&amp; /etc/init.d/mysqld start &amp;&amp; /etc/init.d/httpd start</span><br><span class="line"></span><br><span class="line">RUN mysql -e &quot;grant all on *.* to root@&#x27;%&#x27; identified by &#x27;123456&#x27;;grant all on *.* to root@&#x27;localhost&#x27; identified by &#x27;123456&#x27;;grant all on *.* to discuz@&#x27;%&#x27; identified by &#x27;123456&#x27;;grant all on *.* to discuz@&#x27;localhost&#x27; identified by &#x27;123456&#x27;;create database discuz charset utf8;&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">3 添加脚本启动文件到容器内</span></span><br><span class="line">COPY init.sh  /</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">4 添加bbs文件到http服务根目录下，并测试下载功能</span></span><br><span class="line">ADD DiscuzX.tar /var/wwww/html</span><br><span class="line">ADD /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo /tmp</span><br><span class="line">RUN chmod -R 777 /var/www/html/</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">5 暴露端口</span></span><br><span class="line">EXPOSE 22</span><br><span class="line">EXPOSE 80</span><br><span class="line">EXPOSE 3306</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">6 容器启动时指定的命令</span></span><br><span class="line">CMD [&quot;/bin/bash&quot;,&quot;/init.sh&quot;]</span><br></pre></td></tr></table></figure>

<h4 id="使用dockerfile文件构建镜像"><a href="#使用dockerfile文件构建镜像" class="headerlink" title="使用dockerfile文件构建镜像"></a>使用dockerfile文件构建镜像</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">[root@docker bbs]# docker  build -t &quot;centos6_bbs&quot; ./</span><br><span class="line">···</span><br><span class="line">ERROR 2002 (HY000): Can&#x27;t connect to local MySQL server through socket &#x27;/var/lib/mysql/mysql.sock&#x27; (2)</span><br><span class="line">The command &#x27;/bin/sh -c mysql -e &quot;grant all on *.* to root@&#x27;%&#x27; identified by &#x27;123456&#x27;;grant all on *.* to root@&#x27;localhost&#x27; identified by &#x27;123456&#x27;;grant all on *.* to discuz@&#x27;%&#x27; identified by &#x27;123456&#x27;;grant all on *.* to discuz@&#x27;localhost&#x27; identified by &#x27;123456&#x27;;create database discuz charset utf8;&quot;&#x27; returned a non-zero code: 1</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">报错信息/bin/sh -c mysql是因为调用了/bin/sh下的MySQL命令，但MySQL不是默认的shell命令，因此失败</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">我们将MySQL执行命令放在init.sh脚本中</span></span><br><span class="line">[root@docker bbs]# cat init.sh </span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">/etc/init.d/mysqld start</span><br><span class="line">mysql -e &quot;grant all on *.* to root@&#x27;%&#x27; identified by &#x27;123456&#x27;;grant all on *.* to root@&#x27;localhost&#x27; identified by &#x27;123456&#x27;;grant all on *.* to discuz@&#x27;%&#x27; identified by &#x27;123456&#x27;;grant all on *.* to discuz@&#x27;localhost&#x27; identified by &#x27;123456&#x27;;create database discuz charset utf8;&quot;</span><br><span class="line">/etc/init.d/httpd start</span><br><span class="line">/usr/sbin/sshd -D</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">修改完成后重新执行制作命令</span></span><br><span class="line">[root@docker bbs]# docker build -t &quot;centos6_bbs&quot; ./</span><br><span class="line">Sending build context to Docker daemon  49.31MB</span><br><span class="line">Step 1/11 : FROM centos:centos6.10</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">48650444e419</span></span><br><span class="line">Step 2/11 : RUN mv /etc/yum.repos.d/*.repo /tmp &amp;&amp; echo -e &quot;[ftp6]\nname=ftp6\nbaseurl=ftp://172.17.0.1/centos6.10\nenabled=1\ngpgcheck=0&quot; &gt; /etc/yum.repos.d/ftp6.repo  &amp;&amp; yum clean all &amp;&amp; yum repolist &amp;&amp; yum -y install openssh-server mysql mysql-server php php-mysql httpd</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Using cache</span></span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">163443b96f08</span></span><br><span class="line">Step 3/11 : RUN /etc/init.d/sshd start &amp;&amp; /etc/init.d/sshd stop &amp;&amp; echo &quot;root:123456&quot; | chpasswd &amp;&amp; /etc/init.d/mysqld start &amp;&amp; /etc/init.d/httpd start</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Using cache</span></span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">71532a0cbbda</span></span><br><span class="line">Step 4/11 : COPY init.sh  /</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Using cache</span></span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">011557e246cf</span></span><br><span class="line">Step 5/11 : ADD DiscuzX.tar /var/wwww/html</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Using cache</span></span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">8ae2ce18a6ff</span></span><br><span class="line">Step 6/11 : ADD http://mirrors.aliyun.com/repo/epel-6.repo /tmp</span><br><span class="line">Downloading [==================================================&gt;]     664B/664B</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">dc80368ca4c2</span></span><br><span class="line">Step 7/11 : RUN chmod -R 777 /var/www/html/</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> 13d05bf9d33b</span></span><br><span class="line">Removing intermediate container 13d05bf9d33b</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">e998a08aa223</span></span><br><span class="line">Step 8/11 : EXPOSE 22</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> a5bf69187121</span></span><br><span class="line">Removing intermediate container a5bf69187121</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">18c5b2acc666</span></span><br><span class="line">Step 9/11 : EXPOSE 80</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> 66764f22d9f6</span></span><br><span class="line">Removing intermediate container 66764f22d9f6</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">3ac56c1cb679</span></span><br><span class="line">Step 10/11 : EXPOSE 3306</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> b4553364173c</span></span><br><span class="line">Removing intermediate container b4553364173c</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">3df29b4dafc9</span></span><br><span class="line">Step 11/11 : CMD [&quot;/bin/bash&quot;,&quot;/init.sh&quot;]</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">Running <span class="keyword">in</span> 6ae503afeed2</span></span><br><span class="line">Removing intermediate container 6ae503afeed2</span><br><span class="line"><span class="meta prompt_"> ---&gt; </span><span class="language-bash">73c23cdfae10</span></span><br><span class="line">Successfully built 1a47917b0eb0</span><br><span class="line">Successfully tagged centos6_bbs:latest  # 制作成功</span><br><span class="line"></span><br><span class="line">[root@docker bbs]# docker image ls | grep centos6_bbs</span><br><span class="line">REPOSITORY       TAG         IMAGE ID      CREATED        SIZE</span><br><span class="line">centos6_bbs      latest       1a47917b0eb0 About a minute ago 433MB</span><br></pre></td></tr></table></figure>

<h4 id="验证镜像-1"><a href="#验证镜像-1" class="headerlink" title="验证镜像"></a>验证镜像</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[root@docker bbs]# docker run -d --name centos_bbs -p 8080:80 -p 222:22 -p 13306:3306  1a47917b0eb0</span><br><span class="line">1d4302e50b3a283eba4fb480634969e4e23745eafa42981b0e8b8b639a250c36</span><br><span class="line"></span><br><span class="line">[root@docker ~]# docker exec -it 1d4302e50b3a  /bin/bash</span><br><span class="line">[root@1d4302e50b3a /]# ls /var/www/html/</span><br><span class="line">readme/    README.md  upload/    utility/   </span><br><span class="line">[root@1d4302e50b3a /]# ls /var/www/html/</span><br><span class="line">readme  README.md  upload  utility   </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">打包的bbs文件，应该只包含repload目录即可，其他文件可不打包传递</span></span><br></pre></td></tr></table></figure>

<p>浏览器安装(这里只做测试，不截屏)：</p>
<p>![image-20200720142958094]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720142958094.png)</p>
<p>安装完成后，如下：</p>
<p>![image-20200720143113667]&#x2F;pic&#x2F;docker-pic&#x2F;docker-pic&#x2F;image-20200720143113667.png)</p>
<h2 id="联合文件系统"><a href="#联合文件系统" class="headerlink" title="联合文件系统"></a>联合文件系统</h2><p>​        支持将不同位置的目录挂载到同一虚拟文件系统，形成一种分层的模型。</p>
<p><img src="/pic/docker-pic/image-20210401153224811.png" alt="image-20210401153224811"></p>
<p>实际拓扑图如下：</p>
<p><img src="/pic/docker-pic/image-20210402101829939.png" alt="image-20210402101829939"></p>
<p>写时复制(COW:copy on write)：当需要修改文件时，会先从镜像里把需要写的文件复制到自己的文件系统中进行修改</p>
<p><img src="/pic/docker-pic/image-20210402102121522.png" alt="image-20210402102121522"></p>
<p>读写文件的顺序：先找容器层，如果有则复制到读写层进行读写，如果容器层没有，再去镜像层查找</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container <span class="built_in">ls</span></span></span><br><span class="line">CONTAINER ID   IMAGE          COMMAND   CREATED       STATUS       PORTS     NAMES</span><br><span class="line">60ffca766bde   f1cb7c7d58b7   &quot;bash&quot;    6 weeks ago   Up 2 hours             c1</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker inspect f1cb7c7d58b7    <span class="comment"># 查看镜像f1cb7c7d58b7的信息</span></span></span><br><span class="line">[</span><br><span class="line">    &#123;</span><br><span class="line">        &quot;Id&quot;: &quot;sha256:f1cb7c7d58b73eac859c395882eec49d50651244e342cd6c68a5c7809785f427&quot;,</span><br><span class="line">        &quot;RepoTags&quot;: [</span><br><span class="line">            &quot;centos:centos7.6.1810&quot;</span><br><span class="line">        ],</span><br><span class="line">        &quot;RepoDigests&quot;: [],</span><br><span class="line">        &quot;Parent&quot;: &quot;&quot;,</span><br><span class="line">        &quot;Comment&quot;: &quot;&quot;,</span><br><span class="line">        &quot;Created&quot;: &quot;2019-03-14T21:20:29.635970966Z&quot;,</span><br><span class="line">        &quot;Container&quot;: &quot;cc6043a787f6d1c7ae3e121ebdf1c4478186336aa7274871780a0a7bcc3a061a&quot;,</span><br><span class="line">        &quot;ContainerConfig&quot;: &#123;</span><br><span class="line">            &quot;Hostname&quot;: &quot;cc6043a787f6&quot;,</span><br><span class="line">            &quot;Domainname&quot;: &quot;&quot;,</span><br><span class="line">            &quot;User&quot;: &quot;&quot;,</span><br><span class="line">            &quot;AttachStdin&quot;: false,</span><br><span class="line">            &quot;AttachStdout&quot;: false,</span><br><span class="line">            &quot;AttachStderr&quot;: false,</span><br><span class="line">            &quot;Tty&quot;: false,</span><br><span class="line">            &quot;OpenStdin&quot;: false,</span><br><span class="line">            &quot;StdinOnce&quot;: false,</span><br><span class="line">            &quot;Env&quot;: [</span><br><span class="line">                &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;Cmd&quot;: [</span><br><span class="line">                &quot;/bin/sh&quot;,</span><br><span class="line">                &quot;-c&quot;,</span><br><span class="line">                &quot;#(nop) &quot;,</span><br><span class="line">                &quot;CMD [\&quot;/bin/bash\&quot;]&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;ArgsEscaped&quot;: true,</span><br><span class="line">            &quot;Image&quot;: &quot;sha256:698a0848ee35389ab7b98494bdc60f887c54ddb94fc2326a1fb4eff8895aff43&quot;,</span><br><span class="line">            &quot;Volumes&quot;: null,</span><br><span class="line">            &quot;WorkingDir&quot;: &quot;&quot;,</span><br><span class="line">            &quot;Entrypoint&quot;: null,</span><br><span class="line">            &quot;OnBuild&quot;: null,</span><br><span class="line">            &quot;Labels&quot;: &#123;</span><br><span class="line">                &quot;org.label-schema.build-date&quot;: &quot;20181204&quot;,</span><br><span class="line">                &quot;org.label-schema.license&quot;: &quot;GPLv2&quot;,</span><br><span class="line">                &quot;org.label-schema.name&quot;: &quot;CentOS Base Image&quot;,</span><br><span class="line">                &quot;org.label-schema.schema-version&quot;: &quot;1.0&quot;,</span><br><span class="line">                &quot;org.label-schema.vendor&quot;: &quot;CentOS&quot;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;DockerVersion&quot;: &quot;18.06.1-ce&quot;,</span><br><span class="line">        &quot;Author&quot;: &quot;&quot;,</span><br><span class="line">        &quot;Config&quot;: &#123;</span><br><span class="line">            &quot;Hostname&quot;: &quot;&quot;,</span><br><span class="line">            &quot;Domainname&quot;: &quot;&quot;,</span><br><span class="line">            &quot;User&quot;: &quot;&quot;,</span><br><span class="line">            &quot;AttachStdin&quot;: false,</span><br><span class="line">            &quot;AttachStdout&quot;: false,</span><br><span class="line">            &quot;AttachStderr&quot;: false,</span><br><span class="line">            &quot;Tty&quot;: false,</span><br><span class="line">            &quot;OpenStdin&quot;: false,</span><br><span class="line">            &quot;StdinOnce&quot;: false,</span><br><span class="line">            &quot;Env&quot;: [</span><br><span class="line">                &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;Cmd&quot;: [</span><br><span class="line">                &quot;/bin/bash&quot;</span><br><span class="line">            ],</span><br><span class="line">            &quot;ArgsEscaped&quot;: true,</span><br><span class="line">            &quot;Image&quot;: &quot;sha256:698a0848ee35389ab7b98494bdc60f887c54ddb94fc2326a1fb4eff8895aff43&quot;,</span><br><span class="line">            &quot;Volumes&quot;: null,</span><br><span class="line">            &quot;WorkingDir&quot;: &quot;&quot;,</span><br><span class="line">            &quot;Entrypoint&quot;: null,</span><br><span class="line">            &quot;OnBuild&quot;: null,</span><br><span class="line">            &quot;Labels&quot;: &#123;</span><br><span class="line">                &quot;org.label-schema.build-date&quot;: &quot;20181204&quot;,</span><br><span class="line">                &quot;org.label-schema.license&quot;: &quot;GPLv2&quot;,</span><br><span class="line">                &quot;org.label-schema.name&quot;: &quot;CentOS Base Image&quot;,</span><br><span class="line">                &quot;org.label-schema.schema-version&quot;: &quot;1.0&quot;,</span><br><span class="line">                &quot;org.label-schema.vendor&quot;: &quot;CentOS&quot;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;Architecture&quot;: &quot;amd64&quot;,</span><br><span class="line">        &quot;Os&quot;: &quot;linux&quot;,</span><br><span class="line">        &quot;Size&quot;: 201763773,</span><br><span class="line">        &quot;VirtualSize&quot;: 201763773,</span><br><span class="line">        &quot;GraphDriver&quot;: &#123;</span><br><span class="line">            &quot;Data&quot;: &#123;</span><br><span class="line">       # 统一视图 &quot;MergedDir&quot;: &quot;/var/lib/docker/overlay2/dd2431298d13cb0c936f162d2dcbf0868d9457f686ad0d12c7f1d07db28b10d2/merged&quot;,</span><br><span class="line">       # 容器层  &quot;UpperDir&quot;: &quot;/var/lib/docker/overlay2/dd2431298d13cb0c936f162d2dcbf0868d9457f686ad0d12c7f1d07db28b10d2/diff&quot;,</span><br><span class="line">      # 工作目录 &quot;WorkDir&quot;: &quot;/var/lib/docker/overlay2/dd2431298d13cb0c936f162d2dcbf0868d9457f686ad0d12c7f1d07db28b10d2/work&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;Name&quot;: &quot;overlay2&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;RootFS&quot;: &#123;</span><br><span class="line">            &quot;Type&quot;: &quot;layers&quot;,</span><br><span class="line">            &quot;Layers&quot;: [</span><br><span class="line">                &quot;sha256:89169d87dbe2b72ba42bfbb3579c957322baca28e03a1e558076542a1c1b2b4a&quot;</span><br><span class="line">            ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;Metadata&quot;: &#123;</span><br><span class="line">            &quot;LastTagTime&quot;: &quot;0001-01-01T00:00:00Z&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看UpperDir，没有上面新建的日期目录</span></span><br><span class="line">[root@docker20 ~]# ls   /var/lib/docker/overlay2/dd2431298d13cb0c936f162d2dcbf0868d9457f686ad0d12c7f1d07db28b10d2/diff </span><br><span class="line">anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看容器中的三层</span></span><br><span class="line">[root@docker20 ~]# docker container ls </span><br><span class="line">CONTAINER ID   IMAGE          COMMAND   CREATED       STATUS             PORTS     NAMES</span><br><span class="line">60ffca766bde   f1cb7c7d58b7   &quot;bash&quot;    6 weeks ago   Up About an hour             c1</span><br><span class="line">[root@docker20 ~]# docker inspect 60ffca766bde</span><br><span class="line">···</span><br><span class="line">&quot;GraphDriver&quot;: &#123;</span><br><span class="line">            &quot;Data&quot;: &#123;</span><br><span class="line">                &quot;LowerDir&quot;:    &quot;/var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1-init/diff:/var/lib/docker/overlay2/dd2431298d13cb0c936f162d2dcbf0868d9457f686ad0d12c7f1d07db28b10d2/diff&quot;,</span><br><span class="line">                &quot;MergedDir&quot;: &quot;/var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1/merged&quot;,</span><br><span class="line">                &quot;UpperDir&quot;: &quot;/var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1/diff&quot;,</span><br><span class="line">                &quot;WorkDir&quot;: &quot;/var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1/work&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;Name&quot;: &quot;overlay2&quot;</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">···</span><br><span class="line"></span><br><span class="line">[root@docker20 overlay2]# ls /var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1-init/diff</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看<span class="string">&quot;LowerDir&quot;</span>中的目录信息，etc下的hostname、hosts、resolv.conf这三个文件是挂载宿主机对应文件</span></span><br><span class="line">dev  etc</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看<span class="string">&quot;LowerDir&quot;</span>中的目录/etc/目录中的信息</span></span><br><span class="line">[root@docker20 overlay2]# ls /var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1-init/diff/etc/</span><br><span class="line">hostname  hosts  mtab  resolv.conf    # </span><br><span class="line"></span><br><span class="line">[root@docker20 overlay2]# docker container  ls </span><br><span class="line">CONTAINER ID   IMAGE          COMMAND   CREATED       STATUS             PORTS     NAMES</span><br><span class="line">60ffca766bde   f1cb7c7d58b7   &quot;bash&quot;    6 weeks ago   Up About an hour             c1</span><br><span class="line">[root@docker20 overlay2]# docker exec -it 60ffca766bde bash</span><br><span class="line">[root@60ffca766bde /]# cat /etc/hostname   # 容器的主机名</span><br><span class="line">60ffca766bde</span><br><span class="line">[root@60ffca766bde /]# cat /etc/resolv.conf   # 容器的DNS解析文件，使用的是宿主机的</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Generated by NetworkManager</span></span><br><span class="line">nameserver 10.138.3.2</span><br><span class="line">nameserver 8.8.8.8</span><br><span class="line">[root@60ffca766bde /]# cat /etc/hosts         # 容器的域名解析文件，使用的是宿主机的，并添加了内容 </span><br><span class="line">127.0.0.1	localhost</span><br><span class="line">::1	localhost ip6-localhost ip6-loopback</span><br><span class="line">fe00::0	ip6-localnet</span><br><span class="line">ff00::0	ip6-mcastprefix</span><br><span class="line">ff02::1	ip6-allnodes</span><br><span class="line">ff02::2	ip6-allrouters</span><br><span class="line">172.17.0.2	60ffca766bde</span><br><span class="line"></span><br><span class="line">[root@docker20 ~]# cat /etc/hosts   # 宿主机的域名解析文件</span><br><span class="line">127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4</span><br><span class="line">::1         localhost localhost.localdomain localhost6 localhost6.localdomain6</span><br><span class="line">[root@docker20 ~]# cat /etc/resolv.conf    # 宿主机的DNS解析文件</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Generated by NetworkManager</span></span><br><span class="line">nameserver 10.138.3.2</span><br><span class="line">nameserver 8.8.8.8</span><br><span class="line">[root@docker20 ~]# </span><br><span class="line">docker20</span><br><span class="line"></span><br><span class="line">[root@docker20 ~]# ls /var/lib/docker/overlay2/dd2431298d13cb0c936f162d2dcbf0868d9457f686ad0d12c7f1d07db28b10d2/diff/</span><br><span class="line">anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var   # 这个是容器中根文件下的目录</span><br><span class="line"></span><br><span class="line">[root@60ffca766bde /]# ls   # 在容器里面查看/目录下的文件，跟上文一致</span><br><span class="line">anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">[root@docker20 ~]# ls /   # 查看宿主机的/文件下的目录</span><br><span class="line">bin  boot  data_docker  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line"></span><br><span class="line">[root@60ffca766bde /]# mkdir `date +%F`  # 在容器里面新建目录2021-03-31</span><br><span class="line">[root@60ffca766bde /]# ls </span><br><span class="line">2021-03-31  anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line"></span><br><span class="line">[root@docker20 ~]# ls /var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1/merged</span><br><span class="line">2021-03-31  anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line"></span><br><span class="line">[root@docker20 ~]# ls /var/lib/docker/overlay2/f008794742db7f159b4cf33b581b135e708d1133d729bb676395f92b62d06ba1/diff</span><br><span class="line">2021-03-31  root  usr</span><br></pre></td></tr></table></figure>

<p>如下图：</p>
<p><img src="/pic/docker-pic/image-20210331214339426.png" alt="image-20210331214339426"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io">rushiy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io/docker/docker%E9%95%9C%E5%83%8F/">https://rushiy.gitee.io/docker/docker%E9%95%9C%E5%83%8F/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://rushiy.gitee.io" target="_blank">如是的学习博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/docker/">docker</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">rushiy</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:rushiy@reg.email.cn" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#docker%E5%AE%98%E6%96%B9%E4%BB%93%E5%BA%93%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.</span> <span class="toc-text">docker官方仓库的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#docker%E9%95%9C%E5%83%8F%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4"><span class="toc-number">2.</span> <span class="toc-text">docker镜像相关命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#docker%E9%95%9C%E5%83%8F%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">3.</span> <span class="toc-text">docker镜像常用操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8F"><span class="toc-number">3.1.</span> <span class="toc-text">下载镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E6%89%BE%E9%95%9C%E5%83%8F"><span class="toc-number">3.1.1.</span> <span class="toc-text">查找镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8F-1"><span class="toc-number">3.1.2.</span> <span class="toc-text">下载镜像</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%93%E6%A0%87%E7%AD%BE"><span class="toc-number">3.2.</span> <span class="toc-text">打标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E9%95%9C%E5%83%8F"><span class="toc-number">3.3.</span> <span class="toc-text">删除镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E5%92%8C%E5%AF%BC%E5%87%BA%E9%95%9C%E5%83%8F"><span class="toc-number">3.4.</span> <span class="toc-text">导入和导出镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E8%87%AA%E5%88%B6%E9%95%9C%E5%83%8F%E5%88%B0docker%E5%AE%98%E6%96%B9%E4%BB%93%E5%BA%93"><span class="toc-number">3.5.</span> <span class="toc-text">上传自制镜像到docker官方仓库</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%95%9C%E5%83%8F%E4%BB%93%E5%BA%93"><span class="toc-number">3.5.1.</span> <span class="toc-text">创建镜像仓库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%99%BB%E5%BD%95docker%E5%AE%98%E6%96%B9%E4%BB%93%E5%BA%93"><span class="toc-number">3.5.2.</span> <span class="toc-text">登录docker官方仓库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%93%E6%A0%87%E7%AD%BE-1"><span class="toc-number">3.5.3.</span> <span class="toc-text">打标签</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%A8%E9%80%81%E9%95%9C%E5%83%8F%E5%88%B0docker%E5%AE%98%E6%96%B9%E4%BB%93%E5%BA%93"><span class="toc-number">3.5.4.</span> <span class="toc-text">推送镜像到docker官方仓库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A3%80%E6%9F%A5%E9%95%9C%E5%83%8F%E6%8E%A8%E9%80%81%E7%BB%93%E6%9E%9C"><span class="toc-number">3.5.5.</span> <span class="toc-text">检查镜像推送结果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%95%9C%E5%83%8F%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="toc-number">3.6.</span> <span class="toc-text">查看镜像的详细信息</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E5%AE%B9%E5%99%A8%E5%88%B6%E5%81%9Adocker%E9%95%9C%E5%83%8F"><span class="toc-number">4.</span> <span class="toc-text">基于容器制做docker镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%B6%E4%BD%9C%E4%B8%80%E4%B8%AA%E6%8B%A5%E6%9C%89ssh%E6%9C%8D%E5%8A%A1%E7%9A%84%E9%95%9C%E5%83%8F"><span class="toc-number">4.1.</span> <span class="toc-text">制作一个拥有ssh服务的镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C%EF%BC%9A%E5%88%B6%E4%BD%9C%E6%9C%AC%E5%9C%B0%E5%B1%80%E5%9F%9F%E7%BD%91yum%E6%BA%90"><span class="toc-number">4.1.1.</span> <span class="toc-text">准备工作：制作本地局域网yum源</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%B6%E4%BD%9Ccentos6%E7%9A%84ssh%E6%9C%8D%E5%8A%A1%E9%95%9C%E5%83%8F"><span class="toc-number">4.1.2.</span> <span class="toc-text">制作centos6的ssh服务镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%B6%E4%BD%9Ccentos7%E7%9A%84ssh%E6%9C%8D%E5%8A%A1%E9%95%9C%E5%83%8F"><span class="toc-number">4.1.3.</span> <span class="toc-text">制作centos7的ssh服务镜像</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%BC%81%E4%B8%9A%E7%BD%91%E7%AB%99BBS%E9%95%9C%E5%83%8F"><span class="toc-number">4.2.</span> <span class="toc-text">构建企业网站BBS镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%B6%E4%BD%9Clamp%E7%8E%AF%E5%A2%83"><span class="toc-number">4.2.1.</span> <span class="toc-text">制作lamp环境</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85bbs"><span class="toc-number">4.2.2.</span> <span class="toc-text">安装bbs</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%90%AF%E5%8A%A8%E8%84%9A%E6%9C%AC"><span class="toc-number">4.2.3.</span> <span class="toc-text">创建启动脚本</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E7%BC%96%E5%88%B6dockerfile%E6%96%87%E4%BB%B6%E5%88%B6%E4%BD%9Cdocker%E9%95%9C%E5%83%8F"><span class="toc-number">5.</span> <span class="toc-text">通过编制dockerfile文件制作docker镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#dockerfile%E6%96%87%E4%BB%B6%E5%B8%B8%E7%94%A8%E6%8C%87%E4%BB%A4"><span class="toc-number">5.1.</span> <span class="toc-text">dockerfile文件常用指令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%B6%E4%BD%9Cssh%E6%9C%8D%E5%8A%A1%E7%9A%84centos6%E9%95%9C%E5%83%8F"><span class="toc-number">5.2.</span> <span class="toc-text">制作ssh服务的centos6镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E5%86%99Dockerfile%E6%96%87%E4%BB%B6"><span class="toc-number">5.2.1.</span> <span class="toc-text">编写Dockerfile文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8Dockerfile%E6%96%87%E4%BB%B6%E7%94%9F%E6%88%90%E9%95%9C%E5%83%8F"><span class="toc-number">5.2.2.</span> <span class="toc-text">使用Dockerfile文件生成镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%AA%8C%E8%AF%81%E9%95%9C%E5%83%8F"><span class="toc-number">5.2.3.</span> <span class="toc-text">验证镜像</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%B6%E4%BD%9Clamp%E9%95%9C%E5%83%8F"><span class="toc-number">5.3.</span> <span class="toc-text">制作lamp镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E5%88%B6dockerfile%E6%96%87%E4%BB%B6"><span class="toc-number">5.3.1.</span> <span class="toc-text">编制dockerfile文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8dockerfile%E6%96%87%E4%BB%B6%E6%9E%84%E5%BB%BA%E9%95%9C%E5%83%8F"><span class="toc-number">5.3.2.</span> <span class="toc-text">使用dockerfile文件构建镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%AA%8C%E8%AF%81%E9%95%9C%E5%83%8F-1"><span class="toc-number">5.3.3.</span> <span class="toc-text">验证镜像</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%81%94%E5%90%88%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="toc-number">6.</span> <span class="toc-text">联合文件系统</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django模板系统"/></a><div class="content"><a class="title" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统">Django模板系统</a><time datetime="2022-11-04T07:08:03.000Z" title="发表于 2022-11-04 15:08:03">2022-11-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django视图"/></a><div class="content"><a class="title" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图">Django视图</a><time datetime="2022-11-03T07:07:51.000Z" title="发表于 2022-11-03 15:07:51">2022-11-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django路由系统"/></a><div class="content"><a class="title" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统">Django路由系统</a><time datetime="2022-11-02T07:07:34.000Z" title="发表于 2022-11-02 15:07:34">2022-11-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="走进Django"/></a><div class="content"><a class="title" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django">走进Django</a><time datetime="2022-11-01T07:01:20.000Z" title="发表于 2022-11-01 15:01:20">2022-11-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="python常用模块"/></a><div class="content"><a class="title" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块">python常用模块</a><time datetime="2022-10-28T15:34:07.000Z" title="发表于 2022-10-28 23:34:07">2022-10-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By rushiy</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>